自动化监控运维(四) 基于Prometheus+Grafana监控Nginx

一、 基于nginx-module-vts监控

1.1 下载

https://github.com/vozlt/nginx-module-vts

1.2 重新编译nginx

./configure \
--prefix=/usr/local/nginx-1.25.0 \
--add-module=./nginx-module-vts-0.2.2 \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module

1.3 nginx 配置

http {
#---隐藏其他配置
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
server {
#---隐藏其他的路由
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}

1.4 Prometheus 配置

- job_name: "nginx_vts_status"
metrics_path: '/status/format/prometheus'
scrape_interval: 10s
static_configs:
- targets: ["192.168.1.201:88"]

1.5 配置 grafana

导入模板: https://grafana.com/grafana/dashboards/14824-nginx-vts-stats/

二、 基于nginx-prometheus-exporter监控

2.1 配置nginx

  • 确保当前运行的nginx支持http_stub_status
    ./nginx -V 2>&1 | grep -o with-http_stub_status_module

  • nginx 开启http_stub_status
    vim /path2yournginx/nginx.conf

      server {
    listen 80;
    location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }
    }

2.2 配置nginx-prometheus-exporter

  • 下载
    https://github.com/nginxinc/nginx-prometheus-exporter

  • 解压
    tar -xzvf node_exporter-1.7.0.linux-amd64.tar.gz -C /usr/local/

  • 运行
    ./nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1/nginx_status

2.3 配置 Prometheus

- job_name: "nginx_status"
metrics_path: '/metrics'
scrape_interval: 10s
static_configs:
- targets: ["192.168.1.201:9113"]

2.4 导入模板

https://grafana.com/grafana/dashboards/11199-nginx/