在 Nginx 中,Stub-Status 是一个轻量级的内置模块(需编译时启用),用于暴露基本的运行时连接状态,其中就包括当前活跃的并发连接数。它不依赖外部工具,适合快速查看实时连接情况。
确认 stub_status 模块已启用
Nginx 默认可能未启用 ngx_http_stub_status_module。可通过以下命令验证:
nginx -V 2>&1 | grep -o with-http_stub_status_module
若无输出,说明模块未编译进 Nginx,需重新编译并添加 --with-http_stub_status_module 参数。
配置 stub_status 接口位置
在任意 server 块(推荐使用本地访问的专用 location)中添加如下配置:
location /nginx-status {
stub_status on;
access_log off;
allow 127.0.0.1; # 仅允许本机访问
deny all;
}
解析 stub_status 返回内容
访问 http://127.0.0.1/nginx-status(需 curl 或浏览器,且满足 allow 规则),返回类似:
Active connections: 12 server accepts handled requests 1568 1568 2452 Reading: 0 Writing: 3 Waiting: 9
- Active connections:当前所有活跃连接数(即你关心的“并发连接数”),包含正在读、写、等待的连接
- Reading:Nginx 正在读取客户端请求头的连接数
- Writing:Nginx 正在向客户端发送响应的连接数
- Waiting:处于 keep-alive 状态、空闲等待新请求的连接数(属于 Active 的一部分)
三者关系为:Active = Reading + Writing + Waiting。
结合脚本或监控系统采集
可使用简单命令提取并发数,例如:
curl -s http://127.0.0.1/nginx-status | awk '/Active connections/ {print $3}'
将该命令接入 Prometheus(通过 nginx-exporter 更稳妥)、Zabbix 或自建定时脚本,即可实现持续跟踪与告警。













