Nginx stub_status模块需手动启用并配置location,检查是否编译进系统用nginx -V | grep,配置中须设stub_status on、限制访问、关闭日志,响应含Active connections等六项关键指标,支持脚本解析对接监控系统。

Nginx 的 stub_status 模块提供了一个轻量级的运行状态接口,但默认不启用,需手动配置 location 并确保模块已编译进 Nginx。
确认 stub_status 模块已启用
执行以下命令检查是否包含 http_stub_status_module:
若无输出,说明该模块未编译,需重新编译 Nginx 并添加 --with-http_stub_status_module 参数。大多数官方预编译包(如 Ubuntu/Debian 的 apt 包、CentOS 的 EPEL 包)已默认启用此模块。
配置 location 暴露状态接口
在 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+ 可省略on,但建议显式写出) -
access_log off避免频繁请求刷日志 - 务必限制访问来源(如
allow/deny或配合 auth_basic),否则会暴露服务器内部指标 - 路径名可自定义(如
/status),但不能是已有静态文件或代理路径,避免冲突
访问并解读返回内容
重启 Nginx 后,用 curl 访问配置的路径:
curl http://127.0.0.1/nginx-status典型响应如下:
Active connections: 3server accepts handled requests
12345 12345 67890
Reading: 0 Writing: 1 Waiting: 2
含义说明:
- Active connections:当前活跃连接数(包括 Reading、Writing、Waiting)
- accepts:Nginx 自启动以来接受的总连接数
- handled:成功处理的连接数(通常等于 accepts)
- requests:客户端发起的总请求数(含 keepalive 复用连接中的多个请求)
- Reading:正在读取请求头的连接数
- Writing:正在向客户端发送响应的连接数
- Waiting:处于 keepalive 状态、等待新请求的空闲连接数
配合 Prometheus 或监控脚本使用(可选增强)
stub_status 输出是纯文本,适合简单采集。如需对接 Prometheus,可用 nginx-lua-prometheus(需 Lua 支持),或用轻量脚本解析 stub_status 输出转为 metrics 格式。例如用 awk 提取 active 连接数:
curl -s http://127.0.0.1/nginx-status | awk 'NR==1 {print "nginx_active_connections "$3}'再写入 Telegraf、Zabbix Agent 或自定义 exporter 即可实现持续监控。


















