nginx的监控指标

1. nginx-exporter(基于 Nginx 原生 status 模块)

  • 监控对象
    依赖 Nginx 的 ngx_http_stub_status_module 模块(需在 Nginx 配置中启用)。
  • 配置示例
    server {
      listen 80;
      location /status {
        stub_status on;
        allow 127.0.0.1;
        deny all;
      }
    }
    
  • 指标路径
    metrics_path: /status/format/prometheus(需配合第三方插件如 nginx-prometheus-exporter 转换为 Prometheus 格式)。
  • 暴露的指标
    基础性能指标,例如:
    nginx_connections_active 12
    nginx_connections_reading 1
    nginx_connections_writing 3
    nginx_connections_waiting 8
    nginx_requests_total 48723
    
  • 特点
    • 轻量级,但指标较少。
    • 需额外 exporter 转换格式(原生 /status 返回的是纯文本,非 Prometheus 格式)。

2. nginx-vts-exporter(基于 Nginx VTS 模块)

  • 监控对象
    依赖第三方模块 nginx-module-vts(需编译安装到 Nginx 中)。
  • 配置示例
    http {
      vhost_traffic_status_zone;
      server {
        listen 9913;
        location /status {
          vhost_traffic_status_display;
          vhost_traffic_status_display_format prometheus;
        }
      }
    }
    
  • 指标路径
    直接暴露 Prometheus 格式的指标(metrics_path 默认为 /metrics)。
  • 暴露的指标
    更丰富的流量和虚拟主机指标,例如:
    nginx_vts_server_requests_total{host="example.com"} 10241
    nginx_vts_server_bytes_in{host="example.com"} 5242880
    nginx_vts_server_bytes_out{host="example.com"} 10485760
    nginx_vts_upstream_requests_total{upstream="backend"} 7842
    
  • 特点
    • 功能强大,支持按虚拟主机、上游分组统计。
    • 原生支持 Prometheus 格式,无需额外 exporter。
    • 需重新编译 Nginx 或使用预编译版本。

3. 关键对比

特性 nginx-exporter nginx-vts-exporter
依赖模块 ngx_http_stub_status_module nginx-module-vts
指标丰富度 基础连接/请求指标 详细的虚拟主机/上游流量指标
数据格式 需转换(原生为文本) 原生 Prometheus 格式
安装复杂度 低(默认模块) 高(需编译安装)
典型端口 80(与业务共用) 9913(独立端口)

4. 如何选择?

  • 基础监控:用 nginx-exporter(简单够用)。
  • 高级监控:用 nginx-vts-exporter(需详细流量分析)。
  • 两者共存:可以同时使用,但需注意指标命名冲突(建议添加 metric_relabel_configs 重命名)。
posted on 2025-07-02 22:17  Leo_Yide  阅读(183)  评论(0)    收藏  举报