直接启用 stub_status 模块即可获取 Nginx 实时连接与请求处理数据,输出三行纯文本,轻量低开销;需确认模块已编译支持,配置须在 server 块内、限制访问、关闭日志,并正确解读 Active connections、accepts/handled 差值及 Reading/Writing/Waiting 含义。

直接启用 stub_status 模块就能拿到 Nginx 实时连接与请求处理的核心数据,不需要额外服务或代理。它输出的是固定三行纯文本,轻量、低开销,是判断瞬时压力和定位瓶颈的第一手依据。
确认模块已编译并验证支持
模块没编译进 Nginx,后续所有配置都无效。执行命令验证:
- nginx -V 2>&1 | grep -o with-http_stub_status_module —— 有输出说明已支持
- 若无输出,需重新编译 Nginx,添加 --with-http_stub_status_module 参数
- 注意:不是
--with-http_sub_status_module(旧名/拼写错误)
在 server 块中安全暴露状态接口
必须嵌套在某个 server 块内,不能放在 http 或全局上下文;路径要受控,避免信息泄露:
- 推荐监听本地端口隔离,例如:
listen 127.0.0.1:8081; - 路径避开通用名(如
/status),用/_nxs或/nginx-status - 务必关闭日志:
access_log off; - 严格限制 IP:
allow 127.0.0.1;+ 监控服务器 IP,deny all;
读懂返回内容里的关键信号
访问 curl http://127.0.0.1:8081/_nxs 得到类似输出:
Active connections: 142 server accepts handled requests 156890 156890 423156 Reading: 1 Writing: 28 Waiting: 113
各字段含义与关注点:
- Active connections:当前所有 TCP 连接数(含 keepalive 空闲连接),反映瞬时负载
-
accepts ≠ handled:说明有连接被丢弃,可能因
worker_connections不足或系统 fd 耗尽 - Reading 长期偏高(如 >5% Active):客户端发包慢、网络延迟大,或存在慢速攻击
- Writing 持续高位(如 >30% Active 且波动小):后端响应慢、带宽打满、或响应体过大
- Waiting 占比过低(如长期
接入监控系统实现持续采集与告警
stub_status 原生输出不是指标格式,需转换后才能被 Prometheus、Zabbix 等消费:
- 用 nginx-prometheus-exporter 最省心:启动时指定
-nginx.scrape-uri http://127.0.0.1:8081/_nxs,默认暴露:9113/metrics - Zabbix 可通过
UserParameter+awk提取字段,例如:UserParameter=nginx.waiting, curl -s ... | awk 'NR==5 {print $6}' - 简单场景可用 shell 脚本定时抓取,写入
node_exporter textfile目录,零新增进程 - Prometheus 中可设基础告警,如:
nginx_stub_active_connections > 800 for: 2m











