通过with-http_stub_status_modulenginx-module-vts监控nginx

https://juejin.cn/post/7119751560900378637#heading-6

前言:nginx安装时推荐编译安装

一. nginx编译两模块 (with-http_stub_status_module监控的东西太少不推荐 )

nginx-module-vts下载链接:
https://codeload.github.com/vozlt/nginx-module-vts/tar.gz/v0.1.18

编译安装nginx
https://gegewu12.github.io/2023/05/15/%E7%BC%96%E8%AF%91%E9%83%A8%E7%BD%B2nginx/

1.首次安装nginx编译(with-http_stub_status_module模块)

查看是否已有模块

1
2
# 查看是否已编译
./nginx -V 2>&1 | grep -o with-http_stub_status_module

编译安装

1
cd /usr/local/src/${dir} && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream

2.nginx 已安装但是没有编译nginx-module-vts模块(推荐使用nginx-module-vts监控nginx

nginx-module-vts下载链接:
https://codeload.github.com/vozlt/nginx-module-vts/tar.gz/v0.1.18

nginx下载链接:
http://nginx.org/download/nginx-1.16.1.tar.gz

如果是编译安装则直接将vts模块解压到/usr/local/src/nginx-1.16.1目录使用--add-module=/usr/local/src/nginx-1.16.1/nginx-module-vts-0.1.18指定编译模块

如果是其他安装方式如apt则需要下载与apt安装方式相同版本nginx安装包 解压到/usr/local/src/目录再将vts模块解压到/usr/local/src/nginx-1.16.1目录使用--add-module=/usr/local/src/nginx-1.16.1/nginx-module-vts-0.1.18指定编译模块

框内编译所有操作在/usr/local/src/nginx-1.16.1目录执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 下载nginx压缩包
cd /usr/local/src/nginx-1.16.1
# 将压缩包解压到
wget https://codeload.github.com/vozlt/nginx-module-vts/tar.gz/v0.1.18
tar xvf nginx-module-vts-0.1.18.tar.gz
# 查看已有模块
root@gegewu:/usr/local/src/nginx-1.16.1# /usr/local/nginx/sbin/nginx -V
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream
# 复制nginx -V内容再将vts模块编译进去只make不install (否则会覆盖)
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream --add-module=/usr/local/src/nginx-1.16.1/nginx-module-vts-0.1.18 && make
# 停止nginx
systemctl stop nginx
# 此时编译完成替换新编译的nginx二进制文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/ (objs在nginx压缩包机解压目录)
# 重启 / 重新加载配置
systemctl restart nginx
/usr/local/nginx/sbin/nginx -s reload
# nginx -V 查看VTS模块是否添加成功
root@gegewu:/usr/local/src/nginx-1.16.1/objs# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream --add-module=/usr/local/src/nginx-1.16.1/nginx-module-vts-0.1.18

此时编译成功!

image-20230615155432702

make完成截图

image-20230615160200983

3. docker-compose文件

https://gegewu12.github.io/2023/05/25/telegraf%E9%87%87%E9%9B%86%E7%9B%91%E6%8E%A7%E6%95%B0%E6%8D%AE%E7%BB%84%E4%BB%B6/

二. 监控nginx with-http_stub_status_module模块 ( 监控的东西太少不建议使用 )

1.修改nginx配置文件 server模块部分 (/usr/local/nginx/conf/nginx.conf)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 修改nginx配置文件 server模块部分
# nginx with-http_stub_status_module 模块
location ~ /nginx_status {stub_status on;access_log off;allow all;deny all;}

location /nginx_status {
allow 127.0.0.1;
deny all;
stub_status on;
access_log off;
}
注:如果 需要windows访问还需要 放通windows的IP段
如下:VTS模块参考
# nginx-module-vts模块
location /status {
allow 127.0.0.1;
allow windows-ip/24;
deny all;
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
access_log off;
}

2.此模块可搭配nginx-prometheus-exporter:latest采集插件使用

1
2
# docker run构建监控插件容器
docker run -d --restart always -p 9113:9113 nginx/nginx-prometheus-exporter:latest -nginx.scrape-uri http://10.0.0.10/nginx_status

3.采用nginx-prometheus-exporter采集插件prometheus.yml配置

1
2
3
4
5
# prometheus配置
- job_name: 'nginx-prometheus-exporter'
scrape_interval: 10s
static_configs:
- targets: ['10.0.0.10:9113']

4 采用telegraf采集插件telegraf.conf配置(此处需要写成实际IP不能写为localhost

1
2
3
[[inputs.nginx]]
## An array of Nginx stub_status URI to gather stats.
urls = ["http://10.0.0.10/nginx_status"]

注:此处telegraf nginx-prometheus-exporter选其一

nginx-module-vts模块

1.修改nginx配置文件 server模块部分 (/usr/local/nginx/conf/nginx.conf)

1
2
3
4
5
6
7
8
9
10
11
12
# http模块添加
# 在keepalive_timeout 65;下面添加vhost_traffic_status_zone;
keepalive_timeout 65;
vhost_traffic_status_zone;
# server模块添加:
location /status {
allow 127.0.0.1;
#deny all;
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
access_log off;
}

2. prometheus.yml配置监控

1
2
3
4
5
# nginx 监控
- job_name: 'nginx'
metrics_path: /status/format/prometheus
static_configs:
- targets: ['10.0.0.10:80']

四. 验证

1. with-http_stub_status_module验证:

image-20230615152036398

image-20230615153735318

image-20230616104058752

image-20230615154413627

2. nginx-module-vts验证 (grafana 14900 9785 15205 )

http://10.0.0.10/status

image-20230615162814085

image-20230615163110095

image-20230615164454191