开启nginx_stub_status prometheus


开启nginx_stub_status prometheus

要在Nginx中启用nginx_stub_status并将其数据暴露给Prometheus,你需要执行以下步骤:

  1. 配置Nginx以使用stub_status

  2. 设置Prometheus的exporter来抓取nginx_stub_status页面。

  3. 配置Prometheus服务来抓取exporter的数据。

步骤 1: 配置Nginx

编辑Nginx配置文件,通常是nginx.conf或者单独的虚拟主机配置文件。在server块中添加一个新的location块来定义stub_status

 
 
server {
# ... 其他配置 ...
 
location /nginx_status {
stub_status on; # 开启状态页面
access_log off; # 不记录访问nginx状态页的日志
allow 127.0.0.1; # 只允许本地访问
deny all; # 拒绝其他IP访问
}
}

确保重新加载Nginx配置以使更改生效:

 
 
sudo nginx -s reload

步骤 2: 设置Prometheus Exporter

下载并运行Nginx Exporter:

 
 
wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.9.0/nginx-prometheus-exporter_0.9.0_linux_amd64
chmod +x nginx-prometheus-exporter_0.9.0_linux_amd64
./nginx-prometheus-exporter_0.9.0_linux_amd64 -nginx.scrape-uri=http://127.0.0.1/nginx_status

确保替换127.0.0.1为你的Nginx服务器的实际IP或域名,并且-nginx.scrape-uri指向你的nginx_status页面。

步骤 3: 配置Prometheus

在Prometheus配置文件(通常是prometheus.yml)中,添加一个新的job来指向Nginx Exporter:

 
 
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['localhost:9113']

确保替换localhost:9113为Nginx Exporter运行的实际IP和端口。

重启Prometheus服务以应用更改。

现在,Prometheus将会定期抓取Nginx Exporter,而Nginx Exporter又会从你配置的nginx_status页面抓取数据。你可以通过Prometheus的Web界面查看Nginx的监控数据。

posted @ 2024-09-25 16:49  技术颜良  阅读(116)  评论(0)    收藏  举报