一、安装nginx插件:nginx-module-vts
1.下载nginx源码,这里使用nginx/1.20.1,并上传至服务器
2.下载nginx-module-vts源码,上传至服务器, 解压到/home/op/backup/nginx-module-vts目录
3.查看当前服务器运行的nginx配置
nginx -V
命令执行后打印出信息:
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/svr/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre
提取保留这段配置信息
--prefix=/apps/svr/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre
4.切换到nginx源码目录,重新编译nginx,通过–add-module参数添加vts module
在从步骤3提取的配置上加上
–add-module=/home/op/backup/nginx-module-vts
--prefix=/apps/svr/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --add-module=/home/op/backup/nginx-module-vts
配置
sudo ./configure --prefix=/apps/svr/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --add-module=/home/op/backup/nginx-module-vts
编译安装
sudo make
sudo make install
编写vts-module配置文件vts.conf,并添加到nginx/conf/conf.d/目录:
vhost_traffic_status_zone;
server {
listen 80;
server_name 172.16.22.71;
server_tokens off; # don't show the version number, a security best practice
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
主配置文件在http节点下添加配置
include conf.d/*.conf;
测试nginx是否能启动
sudo sbin/nginx -t
打印如下信息表示成功
nginx: the configuration file /apps/svr/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/svr/nginx/conf/nginx.conf test is successful
启动nginx
sudo sbin/nginx
至此完成nginx-module-vts插件的安装
二、安装nginx-vts-exporter
1.下载安装
nginx-vts-exporter
下载后解压到nginx服务器,/home/op/prometheus/nginx-exporter,更改文件夹名称为nginx-vts-exporter
2.通过systemd对exporter进行管理,实现随系统自动启动
编写配置文件
sudo vi /etc/systemd/system/nginx-exporter.service
文件内容
[Unit]
Description=Nginx VTS Exporter
After=network.target
[Service]
ExecStart=/home/op/prometheus/nginx-exporter/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri=http://172.16.22.71/status/format/json -telemetry.address=:9913
User=op
[Install]
WantedBy=multi-user.target
3.启动服务
#配置重载
sudo systemctl daemon-reload
#服务开启,“nginx-exporter”为配置文件名前缀部分
sudo systemctl enable nginx-exporter
#服务启动
sudo systemctl start nginx-exporter
4.查询服务是否启动成功
sudo systemctl status nginx-exporter
打印如下信息表示成功
nginx-exporter.service - Nginx VTS Exporter
Loaded: loaded (/etc/systemd/system/nginx-exporter.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2021-10-22 18:04:40 CST; 4 days ago
Main PID: 32487 (nginx-vts-expor)
CGroup: /system.slice/nginx-exporter.service
└─32487 /home/op/prometheus/nginx-exporter/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri=http://172.16.22.71/status/format/json -telemetry.a...
10月 22 18:04:40 centos7 systemd[1]: Started Nginx VTS Exporter.
10月 22 18:04:40 centos7 nginx-vts-exporter[32487]: 2021/10/22 18:04:40 Starting nginx_vts_exporter (version=0.10.3, branch=HEAD, revision=8aa2881c7050...58611d04)
10月 22 18:04:40 centos7 nginx-vts-exporter[32487]: 2021/10/22 18:04:40 Build context (go=go1.10, user=root@56ca8763ee48, date=20180328-05:47:47)
10月 22 18:04:40 centos7 nginx-vts-exporter[32487]: 2021/10/22 18:04:40 Starting Server at : :9913
10月 22 18:04:40 centos7 nginx-vts-exporter[32487]: 2021/10/22 18:04:40 Metrics endpoint: /metrics
10月 22 18:04:40 centos7 nginx-vts-exporter[32487]: 2021/10/22 18:04:40 Metrics namespace: nginx
10月 22 18:04:40 centos7 nginx-vts-exporter[32487]: 2021/10/22 18:04:40 Scraping information from : http://172.16.22.71/status/format/json
Hint: Some lines were ellipsized, use -l to show in full.