Nginx需通过ngx_http_stub_status_module模块启用状态页功能,该模块通常已编译进官方二进制包,需在server块中配置location /nginx_status并限制访问;返回内容包含Active connections、accepts、handled、requests及Reading/Writing/Waiting等关键指标。

Nginx 本身不内置状态页,需通过 ngx_http_stub_status_module 模块启用基础连接与请求统计功能。该模块默认已编译进大多数官方二进制包(如 nginx.org 官方源或主流 Linux 发行版仓库安装的 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 参数。
配置 status 位置块
在任一 server 块内(建议放在默认 server 或专用监控 server 中)添加 location 配置:
location /nginx_status {stub_status on;
access_log off;
allow 127.0.0.1;
# allow 192.168.1.0/24; # 可选:允许内网访问
deny all;
}
注意:
• stub_status on 是启用指令(Nginx 1.13.2+ 可省略 on,直接写 stub_status;)
• 必须关闭 access_log 减少干扰
• 使用 allow/deny 严格限制访问来源,避免暴露敏感指标
理解返回内容含义
访问 http://your-domain/nginx_status(需满足 IP 白名单)会返回类似:
server accepts handled requests
12345 12345 67890
Reading: 0 Writing: 2 Waiting: 1
各字段说明:
• Active connections:当前活跃连接数(包括 Reading、Writing、Waiting)
• accepts:Nginx 接收的总 TCP 连接数
• handled:成功处理的连接数(通常等于 accepts)
• requests:客户端发起的 HTTP 总请求数
• Reading:正在读取请求头的连接数
• Writing:正在向客户端发送响应的连接数
• Waiting:处于 keep-alive 等待新请求的空闲连接数
配合监控系统采集(可选增强)
stub_status 提供的是瞬时文本数据,适合用 Prometheus、Zabbix 等工具定时抓取解析。例如:
• 使用 nginx-exporter(推荐)将 stub_status 自动转为 Prometheus 格式指标
• 编写简单 shell + curl 脚本提取数值,推送到 Telegraf 或自建 API
• 注意设置合理采集间隔(如 15s),避免高频请求冲击 Nginx











