Nginx stub_status模块提供基础连接与请求统计,需手动启用并配合外部工具转换为Prometheus格式;默认预编译包通常已包含,源码编译需添加--with-http_stub_status_module参数;配置时应限制访问权限,仅允许可信IP访问/nginx-status路径;其输出为纯文本,需通过脚本或exporter(如nginx-prometheus-exporter)解析为监控指标;但stub_status仅支持全局汇总数据,不支持按upstream、location或状态码等维度细分,如需细粒度指标需结合access_log分析或使用OpenResty等替代方案。

Nginx 自带的 stub_status 模块可提供基础连接与请求统计,但需手动启用并配合外部工具采集,不能直接输出 Prometheus 格式或自动上报。
确认 stub_status 模块已编译启用
运行 nginx -V 2>&1 | grep -o with-http_stub_status_module,若无输出,说明未启用该模块。多数官方预编译包(如 Ubuntu apt、CentOS yum 安装的 nginx)默认包含;若为源码编译,需添加 --with-http_stub_status_module 参数。
在 server 块中配置 stub_status 路由
仅暴露给可信来源,避免公网可访问:
- 在某个
server块内添加位置块,例如:
location /nginx-status {
stub_status on;
access_log off;
allow 127.0.0.1; # 仅允许本机
allow 10.0.1.0/24; # 或指定内网段
deny all;
}重启 Nginx 后,访问 http://localhost/nginx-status 应返回类似:
Active connections: 3 server accepts handled requests 12345 12345 67890 Reading: 0 Writing: 1 Waiting: 2
用 curl 或 exporter 抓取并转换为监控指标
stub_status 输出是纯文本,需解析后供监控系统使用:
- 简单轮询:用脚本定期
curl -s http://127.0.0.1/nginx-status,提取字段(如Active connections、requests)写入日志或推送到时序库 - 推荐方式:部署 nginx-lua-prometheus(需 Lua 支持)或 nginx-prometheus-exporter(独立进程,通过 stub_status 接口拉取并转成 Prometheus 格式)
- 注意:exporter 默认每 5 秒抓一次,需确保 Nginx 的
/nginx-status路由响应快且稳定,避免超时积压
补充建议:启用更细粒度指标需额外方案
stub_status 仅提供全局汇总数据(总连接数、总请求数等),不支持按 upstream、location 或状态码分组。如需以下能力:
- 各 upstream 的成功率、延迟、QPS
- HTTP 状态码分布(2xx/4xx/5xx 计数)
- 请求体大小、响应时间直方图
应考虑:
– 启用 Nginx 的 log_format + access_log,结合 filebeat + Loki/Promtail 或 ELK 采集分析;
– 使用 OpenResty + lua-resty-prometheus 模块做埋点;
– 或迁移到支持原生指标导出的替代品(如 Envoy、Traefik)。



















