关键在于让nginx原始状态转为prometheus可读指标:先启用stub_status模块并配置/nginx_status路径,再运行nginx-prometheus-exporter抓取该地址,最后通过prometheus拉取:9113/metrics实现监控。

用官方 nginx-prometheus-exporter 暴露 Nginx 核心监控数据,关键不是装工具,而是让 Nginx 的原始状态变成 Prometheus 能读懂的指标。它不改 Nginx 本身,只做“翻译”——把 /nginx_status 或 access log 转成标准的 /metrics 文本格式。
第一步:确认并开启 Nginx stub_status
Exporter 默认依赖 Nginx 自带的 ngx_http_stub_status_module,多数预编译包已内置,只需在配置中启用:
- 在 server 块或单独 location 中添加:
location /nginx_status {<br> stub_status on;<br> allow 127.0.0.1;<br> deny all;<br>} - 路径必须是
/nginx_status(Exporter 默认抓取此地址) - 重启 Nginx 后,本地执行
curl http://localhost/nginx_status,应返回类似Active connections: 12<br>server accepts handled requests<br>12345 12345 67890
的三行文本
第二步:部署并运行 nginx-prometheus-exporter
下载对应架构的二进制(如 Linux amd64),解压后直接运行:
- 基础启动命令(监听默认端口
:9113,抓取本地 stub_status):./nginx-prometheus-exporter --nginx.scrape-uri http://127.0.0.1/nginx_status - 若 Nginx 监听非标准端口(如 8080),需补全地址:
--nginx.scrape-uri http://127.0.0.1:8080/nginx_status - 验证是否生效:访问
http://localhost:9113/metrics,看到形如nginx_http_requests_total{status="200",vhost="default"} 15678的指标行即成功
第三步:确认暴露了哪些核心指标
Exporter 将 stub_status 的原始统计映射为多维 Prometheus 指标,重点关注以下几类:
-
nginx_connections_active:当前活跃连接数(对应 stub_status 中的 “Active connections”) -
nginx_http_requests_total:按状态码、虚拟主机等维度拆分的请求数 -
nginx_http_response_count_total:各响应码(2xx/3xx/4xx/5xx)计数器 -
nginx_upstream_response_time_seconds:上游响应延迟直方图(需 access log 模式才完整支持) - 若启用 worker 级监控(v0.12.0+),还可看到
nginx_worker_connections_active{pid="12345"}这类带进程标签的指标
第四步:配合 Prometheus 抓取与使用
Exporter 本身只是“数据出口”,要真正可用,还需 Prometheus 主动拉取:
- 在
prometheus.yml的scrape_configs中添加 job:job_name: 'nginx'<br> static_configs:<br> - targets: ['192.168.1.100:9113'] # 填 exporter 实际 IP 和端口
- 避免写
localhost(Docker/K8s 环境下不可达) - 重载 Prometheus 配置后,在 Web UI → Status → Targets 页面确认该 job 显示为 UP
- 进入 Graph 页面,输入
rate(nginx_http_requests_total[5m])即可查看 QPS 曲线











