prometheus监控nginx

方式一:nginx-module-vts 方式(推荐,无需额外 exporter)

1. 环境准备与依赖安装

复制代码
# 1. 安装基础依赖
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel git wget

# 2. 创建工作目录
mkdir -p /opt/nginx-install && cd /opt/nginx-install

# 3. 下载 Nginx 1.24.0 和 nginx-module-vts 模块
wget http://nginx.org/download/nginx-1.24.0.tar.gz
git clone git://github.com/vozlt/nginx-module-vts.git

# 4. 解压 Nginx 源码包
tar -zxvf nginx-1.24.0.tar.gz
mv nginx-module-vts /usr/local/nginx-module-vts

GitHub 的 git 协议(git://)可能被防火墙拦截,换成 HTTPS 协议,同时使用国内镜像源(如 GitHub 镜像站):

复制代码
# 1. 先删除失败的空目录(如果有)
rm -rf nginx-module-vts

# 2. 使用 HTTPS 协议克隆(优先)
git clone https://github.com/vozlt/nginx-module-vts.git

# 如果依然失败,使用国内镜像站(如 gitee 镜像)
git clone https://gitee.com/mirrors/nginx-module-vts.git

2. 编译安装 Nginx(带 vts 模块)

复制代码
# 进入 Nginx 源码目录
cd /opt/nginx-install/nginx-1.24.0

# 配置编译参数(指定安装路径 + 加载 vts 模块)
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-module-vts/

# 编译并安装(-j 后接CPU核心数,加快编译速度,比如 -j4)
make -j4 && make install

# 验证模块是否加载成功
/usr/local/nginx/sbin/nginx -V | grep nginx-module-vts
# 输出包含 --add-module=/usr/local/nginx-module-vts/ 即表示成功

3. 配置 Nginx 开启状态监控

复制代码
# 备份原有配置文件
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

# 编辑 Nginx 主配置文件
cat > /usr/local/nginx/conf/nginx.conf << 'EOF'
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    # 开启 vts 状态监控区域
    vhost_traffic_status_zone;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        # 配置状态监控接口
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
EOF

# 启动/重启 Nginx
/usr/local/nginx/sbin/nginx -t  # 验证配置文件
/usr/local/nginx/sbin/nginx     # 启动
# 如果已启动,执行重启:/usr/local/nginx/sbin/nginx -s reload

# 验证状态页面是否可访问
curl http://172.16.213.29/status          # 查看 HTML 格式状态
curl http://172.16.213.29/status/format/prometheus  # 查看 Prometheus 格式指标

4. 配置 Prometheus 采集 Nginx 指标

复制代码
# 备份 Prometheus 配置文件
cp /usr/local/prometheus/prometheus.yml /usr/local/prometheus/prometheus.yml.bak

# 编辑 Prometheus 配置,添加 Nginx 采集任务
sed -i '/scrape_configs:/a\  - job_name: "nginx"\n    metrics_path: /status/format/prometheus\n    static_configs:\n      - targets: ["172.16.213.29"]' /usr/local/prometheus/prometheus.yml

# 验证配置文件
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

# 重启 Prometheus(根据你的启动方式选择,以下是 systemd 方式示例)
systemctl restart prometheus
# 如果是直接启动,先杀死进程再重启:
# ps -ef | grep prometheus | grep -v grep | awk '{print $2}' | xargs kill
# /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &

方式二:nginx-prometheus-exporter 方式(官方推荐,更简单)

1. 确保 Nginx 已开启 stub_status 模块

复制代码
# 编辑 Nginx 配置,开启 stub_status
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
sed -i '/server {/a\        location /basic_status {\n            stub_status on;\n            allow 127.0.0.1;\n            allow 172.16.213.0/24;\n            deny all;\n        }' /usr/local/nginx/conf/nginx.conf

# 验证配置并重启
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

# 验证 stub_status 是否可用
curl http://172.16.213.29/basic_status
# 正常输出示例:
# Active connections: 1 
# server accepts handled requests
#  1 1 1 
# Reading: 0 Writing: 1 Waiting: 0

2. 用 Docker 运行 nginx-prometheus-exporter

复制代码
# 拉取官方镜像
docker pull nginx/nginx-prometheus-exporter:latest

# 启动 exporter(关联 Nginx 的 stub_status 地址)
docker run -d --name nginx-exporter \
  -p 9113:9113 \
  nginx/nginx-prometheus-exporter:latest \
  --nginx.scrape-uri=http://172.16.213.29/basic_status

# 验证 exporter 是否正常输出指标
curl http://172.16.213.29:9113/metrics

3. 配置 Prometheus 采集 exporter 指标

复制代码
# 编辑 Prometheus 配置,添加 exporter 采集任务
sed -i '/scrape_configs:/a\  - job_name: "nginx-exporter"\n    static_configs:\n      - targets: ["172.16.213.29:9113"]' /usr/local/prometheus/prometheus.yml

# 验证配置并重启 Prometheus
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml
systemctl restart prometheus

总结

  1. nginx-module-vts 方式:无需额外运行 exporter,直接通过 Nginx 自身模块暴露 Prometheus 格式指标,指标更丰富(支持虚拟主机、upstream 等)。
  2. nginx-prometheus-exporter 方式:官方维护,部署更简单,仅依赖 Nginx 原生的 stub_status 模块,适合基础监控需求。
  3. 核心验证步骤:确保 Nginx 状态接口可访问 → Prometheus 配置正确 → Prometheus 页面能看到 nginxnginx-exporter 任务状态为 UP

执行完上述命令后,你可以在 Prometheus Web UI(默认 http://prometheus服务器IP:9090)的「Targets」页面查看 Nginx 采集任务是否正常,也可以通过「Graph」页面查询 Nginx 相关指标(如 nginx_connections_activenginx_requests_total 等)。

正确的验证操作

  1. 用浏览器访问 Prometheus UI

在你的电脑(能访问 192.168.52.129 的机器)上打开浏览器,输入:

复制代码
http://192.168.52.129:9090/targets
  • 你会看到 Prometheus 的 Targets 页面,里面会列出 3 个采集任务:prometheusredisnginx_vts
  • 检查nginx_vts的状态:
    • 如果显示UP:说明采集成功
    • 如果显示DOWN:大概率是 Prometheus 访问 Nginx 时的网络问题(但你 curl 能通,大概率是 UP)
  1. 验证是否采集到 Nginx 指标

在浏览器中访问:

复制代码
http://192.168.52.129:9090/graph
  • 在输入框中输入指标名:nginx_vts_main_connections
  • 点击Execute按钮,就能看到 Nginx 的连接数指标数据(如果能看到,说明采集成功)
相关推荐
迷途呀6 小时前
CI/CD 自动化部署实践:让代码提交后自动上线
linux·运维·nginx·ci/cd·自动化
彧azz11 小时前
Git 入门实操实例:从安装到远程仓库全流程
大数据·笔记·git·学习·elasticsearch
醉颜凉12 小时前
Canal 与 Elasticsearch 实时同步:增量索引更新、删除处理与全量重建方案
elasticsearch·canal·数据同步·增量更新·全量重建
无风听海19 小时前
深入解析 Elasticsearch 的 match_phrase_prefix与 match_bool_prefix
大数据·elasticsearch·mybatis
运维全栈笔记19 小时前
Ansible 自动化 Nginx 集群部署实战:动态 Inventory、滚动发布与 Role 工程化
nginx·自动化·ansible
天天喝旺仔20 小时前
Elasticsearch 全文检索实战:Lucene 倒排索引与 NoSQL 文档检索落地
elasticsearch·搜索引擎·全文检索·nosql·lucene
浅念-1 天前
一文吃透Git:本地操作|冲突处理|远程协作|GitFlow工作流详解
大数据·git·elasticsearch·搜索引擎·gitflow
vx-程序开发1 天前
【计算机毕设】基于Spring Boot的古城景区管理系统88564
java·数据库·spring boot·后端·spring·elasticsearch·课程设计
czhc11400756631 天前
2026-09-11 一日综合:树的父链、git 概念、三方死锁、静默失败
大数据·git·elasticsearch
阿标在干嘛2 天前
Nginx一个配置错误,流量损失30%!这是我们的完整排查记录
运维·nginx