可通过Nginx stub_status模块实时查看连接状态,需确认模块已编译并配置location启用,限制访问后curl可获取Active connections等指标,其中活跃工作连接数≈Reading+Writing,建议结合监控持续采集与告警。

可以通过 Nginx 自带的 stub_status 模块快速查看实时连接状态,包括当前活跃连接数、已接受/处理/请求总数等关键指标。前提是该模块已编译进 Nginx(主流发行版默认包含),且在配置中正确启用。
确认 stub_status 模块是否可用
运行以下命令检查模块是否存在:
nginx -V 2>&1 | grep -o with-http_stub_status_module若输出 with-http_stub_status_module,说明已支持;若无输出,需重新编译 Nginx 并添加 --with-http_stub_status_module 参数。
配置 location 启用 stub_status
在 server 块中添加一个专用 location,例如:
location /nginx-status {stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
注意:
- stub_status on 是必需指令(Nginx 1.9.2+ 可简写为 stub_status)
- 建议限制访问来源(如只允许本机或监控服务器),避免敏感信息泄露
- 不要放在 root 或 public location 下,防止被外部扫描到
访问并解读返回结果
重启 Nginx 后,执行:
curl http://127.0.0.1/nginx-status典型输出如下:
Active connections: 3server accepts handled request
12345 12345 67890
Reading: 0 Writing: 1 Waiting: 2
各字段含义:
- Active connections:当前所有 TCP 连接数(包括 keepalive 空闲连接)
- accepts:Nginx 启动以来接受的总连接数
- handled:成功处理的连接数(通常等于 accepts)
- requests:客户端发起的 HTTP 总请求数(含 pipeline 请求)
- Reading:正在读取请求头的连接数
- Writing:正在向客户端发送响应的连接数
- Waiting:处于 keepalive 状态、等待新请求的空闲连接数
→ 所以「当前活跃工作连接数」≈ Reading + Writing
结合监控做持续观察
仅看单次输出意义有限,建议配合工具长期采集:
- 使用 curl -s http://127.0.0.1/nginx-status | awk 'NR==1 {print $3}' 提取 Active connections 数值
- 接入 Prometheus(通过 nginx-vts-exporter 或 nginx-prometheus-exporter)
- 配置告警:当 Active connections 持续高于并发能力阈值(如 > 1000),可能预示连接堆积或后端延迟



















