关键是在编译阶段集成--with-http_stub_status_module和nginx-module-vts模块,配置/metrics等安全限流接口,确保Nginx输出结构化、多维、可被Prometheus稳定采集的监控数据。
安装 Nginx 时要为后续集群状态监控与指标收集打好基础,关键不是“装完再补”,而是在编译或初始化阶段就集成可采集的状态能力。核心是让每台 Nginx 实例能输出结构化、带维度的运行数据,并确保这些数据安全、稳定、可被自动化工具抓取。
若采用源码编译(推荐用于生产集群),必须显式启用监控所需模块:
--with-http_stub_status_module,提供基础连接与请求计数(active connections, accepts/handled/requests)server_name / upstream / location 多维分组,含响应时间分布、缓存命中率、各状态码明细等——这是 Prometheus 或 Telegraf 能做精准下钻的前提./configure --with-http_stub_status_module --add-module=/path/to/nginx-module-vts --prefix=/usr/local/nginx
随后执行 make && make install
若用 apt 或 yum 安装(如 Ubuntu 的 nginx-full、CentOS 的 nginx-mod-http-stub-status):
nginx -V 2>&1 | grep -o with-http_stub_status_module;若无,需切换为编译安装或启用第三方仓库(如 nginx.org 最新 repo)--add-dynamic-module 编译为 .so 动态模块,再在 nginx.conf 中用 load_module 加载无论用 stub_status 还是 vts,配置 location 是落地关键:
access_log off; 避免监控请求污染访问日志allow 10.10.0.0/16; + deny all;,仅放行监控采集器所在网段(如 Prometheus Server 或 Telegraf 节点 IP)location /metrics(vts 的 JSON 接口)或 location /nginx_status(stub_status),便于采集器批量发现env 或注释注明角色(role=lb-edge)、机房(zone=shanghai),后续采集时可通过 relabel 注入 Prometheus 标签安装配置完成后,立即验证是否满足监控链路起点要求:
curl -s http://localhost/metrics | head -20(vts)或 curl -s http://localhost/nginx_status(stub_status)curl -I http://nginx-node-01.example.com/metrics,确认返回 200 OK 且无认证/网络拦截Content-Type: application/json(vts)或纯文本(stub_status),这对 exporter 解析至关重要