nginx_exporter:Prometheus 监控 Nginx 基础指标
nginx_exporterPrometheus 监控 Nginx 基础指标原文https://github.com/discordianfish/nginx_exporterdiscordianfish 维护说明安装方式和配置参数以官方仓库为准。源码细节请参考 GitHub 仓库。备选方案如果需要更丰富的 Nginx 指标可以看 nginx-vts-exporter 或 Nginx 官方出的 nginx-prometheus-exporter。项目简介这是一个轻量级的 Nginx Prometheus Exporter核心功能就是采集 Nginx 状态页面nginx_status的数据然后转成 Prometheus 格式暴露出来。它做的事情很简单定期从 Nginx 的stub_status页面抓取基础统计把数据转成 Prometheus 指标的格式在9113端口通过/metrics暴露前置开启 Nginx stub_status在 Nginx 配置里加一个 location 来暴露状态页server { listen 127.0.0.1:8080; location /nginx_status { stub_status on; allow 127.0.0.1; deny all; } }配置完重载 Nginxnginx-sreload然后访问http://127.0.0.1:8080/nginx_status确认能看到类似下面的内容Active connections: 2 server accepts handled requests 12345 12345 67890 Reading: 0 Writing: 1 Waiting: 1有这些输出说明 stub_status 生效了。安装与启动Docker 方式dockerrun-d-p9113:9113 fish/nginx-exporter\-nginx.scrape_urihttp://172.17.42.1:8080/nginx_statusDocker 里172.17.42.1是默认的宿主机网关地址如果你改了 Docker 网络配置记得换成实际的宿主 IP。二进制方式从 Releases 页面下载对应平台的二进制文件wgethttps://github.com/discordianfish/nginx_exporter/releases/...tarxvf nginx_exporter_*.tar.gz ./nginx_exporter-nginx.scrape_urihttp://127.0.0.1:8080/nginx_status默认监听9113端口。配置参数参数默认值说明-nginx.scrape_urihttp://127.0.0.1:8080/nginx_statusNginx 状态页的 URL-web.listen-address:9113Exporter 监听地址-web.telemetry-path/metrics指标路径查看所有参数./nginx_exporter--help收集的指标这个 exporter 采集的指标比较基础主要来自 Nginx 的stub_status指标含义nginx_connections_active当前活跃连接数nginx_connections_accepted_total从启动到现在累计接受的连接数nginx_connections_handled_total从启动到现在累计成功处理的连接数nginx_connections_reading正在读取请求头的连接数nginx_connections_writing正在写响应的连接数nginx_connections_waiting空闲长连接数nginx_http_requests_total从启动到现在累计处理的请求数常用 PromQL# Nginx 是否在线 nginx_up # 当前活跃连接数 nginx_connections_active # 每秒请求数 rate(nginx_http_requests_total[5m]) # 成功处理率接近 100% 才正常 nginx_connections_handled_total / nginx_connections_accepted_total * 100 # 活跃 vs 空闲连接 nginx_connections_active - nginx_connections_waiting和其他 Nginx Exporter 的对比方案优点缺点discordianfish/nginx_exporter极简只依赖 stub_status指标少只有基础连接和请求nginx-vts-exporter指标更丰富每台虚拟主机、每个 upstream需要装 VTS 模块nginxinc/nginx-prometheus-exporter官方维护Nginx Plus 有很多高级指标开源版指标也少怎么选只是简单看看 Nginx 死活和 QPS → 用这个就够了需要看到每个域名的流量 → 考虑 VTS 方案用 Nginx Plus → 直接上 nginx-prometheus-exporter