时间:2022-03-21来源:www.pcxitongcheng.com作者:电脑系统城
监控Nginx主要用到以下三个模块:
| 1 2 3 |
解压unzip nginx-module-vts-master.zipmv nginx-module-vts-master /usr/local/ |
| 1 2 3 4 5 6 |
tar zxvf nginx-1.15.7.tar.gzcd nginx-1.15.7./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --add-module=/usr/local/nginx-module-vts-mastermake && make install |
修改nginx配置文件,新起一个vhost暴露给server端访问数据:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vim /usr/local/nginx/conf/nginx.confserver下添加如下:http { vhost_traffic_status_zone; --添加 ... server { ... location /status { vhost_traffic_status_display; --添加 vhost_traffic_status_display_format html; --添加 } }} |
Nginx-module-vts模块介绍:
这是一个Nginx模块,提供对虚拟主机状态信息的访问。它包含当前状态,例如servers, upstreams, caches。这类似于nginx plus的实时活动监视。内置的html和旧版本的演示页面也保持一致。这个模块主要就是来监控nginx虚拟主机状态的。
首先,指令vhost_traffic_status_zone是必需的,如果指令vhost_traffic_status_display被设置,可以通过下方式访问:
/status/format/json
请求/status/format/json将用一个包含当前活动数据的json文档进行响应,以便在实时仪表板和三方监视工具中使用。
/status/format/html
请求/status/format/html将会用一个内置的内置的html仪表板网页进行响应,该仪表盘的内部请求走/status/format/json。
/status/format/jsonp
请求/status/format/jsonp将用一个jsonp回调函数进行响应,该函数包含用于实时仪表板和三方监视工具的当前活动数据。
/status/format/prometheus
请求/status/format/prometheus将用包含当前活动数据的prometheus文档来响应。
/status/control
请求/status/control将返回基于查询语句字符串重置或删除区域后的JSON文档。更多可以参考Control.
测试nginx配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果正确没问题,启动nginx
启动nginx:
/usr/local/nginx/sbin/nginx
此时访问http://IP地址/status可以看到nginx的状态信息了。

| 1 2 3 4 5 6 7 8 9 10 |
https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gztar -zxvf nginx-vts-exporter-0.9.1.linux-amd64.tar.gz mv nginx-vts-exporter-0.9.1.linux-amd64 /usr/local/nginx-vts-exporterchmod +x /usr/local/nginx-vts-exporter-0.5/bin/nginx-vts-exportercd /usr/local/nginx-vts-exporter/bin通过nginx-vts-exporter二进制文件来执行nginx-vts-exporter程序nohup ./nginx-vts-exporter -nginx.scrape_uri http://10.10.xx.xx:80/status/format/json &#注意:http://10.10.xx.xx/status/format/json这个地方的ip地址是nginx的IP地址nginx-vts-exporter的监听端口是9913 |
也可以使用systemctl管理nginx-vts-exporter进程。
| 1 2 3 4 5 6 7 8 9 10 11 12 |
[root@localhost nginx-vts-exporter]# vim /usr/lib/systemd/system/nginx_vts_exporter.service [Unit]Description=prometheus_nginx_vtsAfter=network.target[Service]Type=simpleExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri http://10.10.xx.xx:80/status/format/jsonRestart=on-failure[Install]WantedBy=multi-user.target |
| 1 2 3 4 5 6 7 8 9 10 |
[root@localhost nginx-vts-exporter]# systemctl daemon-reload[root@localhost nginx-vts-exporter]# systemctl enable nginx_vts_exporter[root@localhost nginx-vts-exporter]# systemctl start nginx_vts_exporter[root@localhost nginx-vts-exporter]# systemctl status nginx_vts_exporter● nginx_vts_exporter.service - prometheus_nginx_vts Loaded: loaded (/usr/lib/systemd/system/nginx_vts_exporter.service; disabled; vendor preset: disabled) Active: active (running) since Fri xxxx-xx-xx xx:xx:xx EDT; 4 days ago Main PID: 90274 (nginx-vts-expor) CGroup: /system.slice/nginx_vts_exporter.service └─90274 /usr/local/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri http://10.10.xx.xx:80/status/format/json |
添加如下job:
| 1 2 3 4 |
- job_name: 'nginx' scrape_interval: 5s static_configs: - targets: ['192.168.124.16:9913'] |
kubectl apply -f prometheus-cfg.yaml
kubectl delete -f prometheus-deploy.yaml
kubectl apply -f prometheus-deploy.yaml
#注意: - targets: [‘10.10.xx.xx:9913’]这个ip地址是nginx-vts-exporter程序所在机器的ip地址

到此这篇关于prometheus监控nginx的实现的文章就介绍到这了
2024-07-07
myeclipse怎么导入tomcat教程2024-07-07
myeclipse如何启动tomcat2024-07-07
myeclipse如何绑定tomcat上线了一个小的预约程序,配置通过Nginx进行访问入口,默认的日志是没有请求时间的,因此需要配置一下,将每一次的请求的访问响应时间记录出来,备查与优化使用....
2023-03-17