要监控 Nginx,常见方案是给 Nginx 编译进 nginx-module-vts 模块,再用 nginx-vts-exporter 把 VTS 暴露的指标采集成 Prometheus 能抓的格式,最后在 Grafana 里出图。这里把整套流程记一遍。
一、检查并编译 vts 模块
先看 Nginx 装了哪些模块。RPM 装的直接执行:
nginx -V
源码编译的要进安装目录执行,例如:
/usr/local/nginx/sbin/nginx -V
没带 vts 的话就需要自己编译。去 GitHub 上 vozlt/nginx-module-vts 拉源码,注意编译是在 Nginx 源码解压目录里做,不是编译好的安装目录:

#下载模块源码进行编译添加,注意进行编译是进入到nginx压缩包解压后的目录,不是nginx编译好的安装目录
[root@nginx ~]# cd nginx-1.20.0
[root@nginx nginx-1.20.0]# git clone git://github.com/vozlt/nginx-module-vts.git
#特别注意添加新模块时,之前已经添加的模块也必须添加进去一块执行命令
[root@nginx nginx-1.20.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/root/nginx-1.20.0/nginx-module-vts/
#执行编译
[root@nginx nginx-1.20.0]# make
#特别注意:千万不要执行make install,如果是已经运行的nginx执行了make install则会把当前的nginx配置全部给覆盖掉
#备份原来的nginx可执行文件
[root@nginx nginx-1.20.0]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
#将编译好的nginx复制到原始nginx的sbin目录
执行这条命令,可能会报以下异常,提示文件被占用:
cp objs/nginx /usr/local/nginx/sbin/nginx
cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy
#可以使用以下命令进行强制覆盖:
[root@nginx nginx-1.20.0]# cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
#再执行/usr/local/nginx/sbin/nginx -V查看一下nginx的模块情况
[root@nginx nginx-1.20.0]# /usr/local/nginx/sbin/nginx -V
这里有个坑:千万不要执行 make install,否则会把正在运行的 Nginx 配置整个覆盖掉。正确做法是备份原来的可执行文件,再用 cp -rfp 强制覆盖新编译出来的二进制。
模块编好、二进制替换完后,平滑升级到新版:先给旧主进程发 -USR2 信号启动新版,再把旧版工作进程用 -WINCH 优雅关掉:
[root@nginx ~]# ps -ef|grep nginx | grep master
root 16107 1 0 Oct29 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# kill -USR2 16107
[root@nginx ~]# ps -ef|grep nginx | grep master
[root@nginx ~]# kill -WINCH 16107

二、加 Nginx 配置
在 http 段里开 VTS 统计区,再加一个对外暴露 /status 的 server,重载后浏览器访问 IP:30080/status 就能看到统计页:
[root@nginx nginx-1.20.0]# vim /usr/local/nginx/conf/nginx.conf
http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
server {
listen 30080;
server_name 192.168.66.68;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
#重载配置
[root@nginx nginx-1.20.0]# nginx -t
[root@nginx nginx-1.20.0]# nginx -s reload

如果默认指标粒度不够,可以在 server 里额外加按 uri、按国家、按 http code、按后端、按端口、按来源 IP 分类统计的过滤项:
server {
listen 80;
server_name kibana.xxx.com;
vhost_traffic_status_filter_by_set_key $uri uri::$server_name; #每个uri访问量
vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name; #不同国家/区域请求量
vhost_traffic_status_filter_by_set_key $status $server_name; #http code统计
vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend; #后端转发统计
vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name; #请求端口统计
vhost_traffic_status_filter_by_set_key $remote_addr client::addr::$server_name; #请求IP统计
location / {
root index index.htm index.html;
}
}
三、部署 exporter
在被监控的 Nginx 机器上下载 nginx-vts-exporter(当前用的是 v0.10.3)并解压到 /usr/local:
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv nginx-vts-exporter-0.10.3.linux-amd64 nginx-vts-exporter

写一个 systemd 服务,把 -nginx.scrape_uri 指向上面的 /status/format/json,启动后访问 IP:9913/metrics 就能看到暴露的指标:
vim /usr/lib/systemd/system/nginx-vts-exporter.service
[Unit]
Description=prometheus_nginx_vts
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri http://192.168.66.68:30080/status/format/json
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx-vts-exporter.service
systemctl start nginx-vts-exporter.service
systemctl status nginx-vts-exporter.service
四、Prometheus 抓取与 Grafana 出图
在 prometheus.yml 里加一个 job 指向 :9913,用 relabel 把主机名和 IP 带出来,检查配置无误后热重载:
#添加Nginx-vts-export相关监控配置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
## Nginx监控
- job_name: 'nginx'
scrape_interval: 30s
static_configs:
- targets: ['192.168.66.68:9913']
relabel_configs:
- source_labels: [__address__]
regex: '(.*):9913'
target_label: 'hostname'
replacement: '$1'
- source_labels: [__address__]
regex: '(.*):9913'
target_label: 'nodeip'
replacement: '$1'
#检查配置是否正确
[root@prometheus ~]# promtool check config /usr/local/prometheus/prometheus.yml
#重载prometheus配置
[root@prometheus ~]# curl -X POST http://127.0.0.1:9090/-/reload
数据源接好后,直接导入 Grafana 上编号 2949 的 Nginx VTS 仪表盘模板就能出图。















暂无评论内容