Prometheus系列之监控Nginx服务

prometheus监控nginx有两种方式。一种是通过nginx_exporter监控,需要开启nginx_stub_status,主要是nginx自身的status信息,metrics数据现对较少;另外一种使用nginx-vts-exporter监控,但是需要在编译nginx的时候添加nginx-module-vts模块,监控数据较多,提供了包含serverupstream以及cache的相关监控指标,指标更丰富,所以采用nginx-vts-exporter实现对nginx的监控。

环境信息

Nginx : openresty 1.27.1 Prometheus : 3.2.1 operating system : RockyLinux 9.5 Mini

nginx-vts-exporter

  1. 下载nginx-module-vts模块
    [root@openresty-dev software]#  git clone https://github.com/vozlt/nginx-module-vts.git
    
  2. 编译nginx添加nginx-module-vts模块支持
    [root@openresty-dev software]#  ./configutre --prefix=/usr/local/openresty ...现有module..  --add-module=/data/software/nginx-module-vts
    
  3. 修改vhost配置文件,添加nginx-module-vts相关配置
    [root@openresty-dev software]# vim /usr/local/openresty/nginx/conf/nginx.conf
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
            location /status {
                vhost_traffic_status_display;
                vhost_traffic_status_display_format html;
    
            }
    ...
    [root@openresty-dev software]# nginx  -t
    [root@openresty-dev software]# nginx -s reload
    

​ 重载nginx配置之后,在浏览器中访问http://<ip或者domain>:port/status可以看到如下指标信息就是成功了。 4. 安装nginx-vts-exporter

[root@openresty-dev software]#  wget https://github.com/sysulq/nginx-vts-exporter/releases/download/v0.10.8/nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz
[root@openresty-dev software]# mv nginx-vtx-exporter /usr/local/
[root@openresty-dev software]# chmod +x /usr/local/nginx_exporter/nginx-vtx-exporter
  1. 配置nginx-vts-exporter systemctl
    [root@openresty-dev software]# vim /usr/lib/systemd/system/nginx-exporter.service
    [Unit] 
    Description=nginx_vts_exporter
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/local/nginx_exporter/nginx-vtx-exporter  -nginx.scrape_uri http://192.168.3.232/status/format/json
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
    [root@openresty-dev software]# systemctl daemon-reload
    [root@openresty-dev software]# systemctl start nginx-exporter
    [root@openresty-dev software]# systemctl enable nginx-exporter
    
  2. 添加配置到prometheus.yml
      - job_name: 'nginx'
        static_configs:
        - targets: ['192.168.3.232:9913']
          labels:
             instance: nginx
    
    然后执行systemctl restart prometheus 重新启动prometheus
  3. grafana导入nginx-exporter监控模板 模板连接:https://grafana.com/grafana/dashboards/2949

nginx_stub_status

This module is not built by default, it should be enabled with the --with-http_stub_status_module configuration parameter.

  1. 编译模块到nginx

    [root@openresty-dev software]#  ./configutre --prefix=/usr/local/openresty ...现有module..  --with-http_stub_status_module
    
  2. 修改nginx.conf配置文件

    ...
    server {
    ....
    location /nginx_status {
       stub_status;
    }
    }
    ...
    [root@openresty-dev software]#  nginx -t && nginx -s reload
    

    浏览器中访问http://<ip或domain>:port/nginx_status

  3. 配置nginx_vts_exporter

    [Unit]
    Description=nginx_vts_exporter
    After=network.target
    
    [Service]
    Type=simple
    #ExecStart=/usr/local/nginx_exporter/nginx-vtx-exporter  -nginx.scrape_uri http://192.168.3.232/status/format/jsonExecStart=/usr/local/nginx_exporter/nginx-vtx-exporter  -nginx.scrape_uri http://192.168.3.232/nbginx_status
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
  4. 查看prometheus采集信息

  5. 添加配置到prometheus.yml

      - job_name: 'nginx'
        static_configs:
        - targets: ['192.168.3.232:9913']
          labels:
             instance: nginx
    

    然后执行systemctl restart prometheus 重新启动prometheus

  6. 进入grafana导入监控模板,就可以了。