当我们需要监控 Nginx 服务器的状态时,可以使用 Prometheus 进行配置。以下是一些简单的步骤:
安装 Prometheus:
$ wget https://github.com/prometheus/prometheus/releases/download/v2.21.0/prometheus-2.21.0.linux-amd64.tar.gz $ tar -xvf prometheus-2.21.0.linux-amd64.tar.gz安装和启动 node_exporter:
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz $ tar -xvf node_exporter-1.0.1.linux-amd64.tar.gz $ ./node_exporter &配置 Nginx Exporter:
$ git clone https://github.com/nginxinc/nginx-prometheus-exporter.git $ cd nginx-prometheus-exporter/ $ make && sudo make install修改 Nginx 的配置文件:
添加以下内容到 /etc/nginx/nginx.conf 文件中的 http 块内:
# 开启 metrics 的路径,默认为 "/metrics" location /metrics { stub_status on; access_log off; allow 127.0.0.1; deny all; }启动 Prometheus 和 Nginx Exporter:
在 Prometheus 的配置文件(prometheus.yml)中添加以下内容:
”`
- job_name: 'nginx' scrape_interval: 5s static_configs: - targets: ['localhost:9113']然后启动 Prometheus 和 Nginx Exporter:\( ./prometheus & \) nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:80/metrics & “`
此时,你可以访问http://localhost:9090/graph 来查看 Prometheus 的数据面板。在查询输入框中输入 “nginx” 即可查看 Nginx 相关的指标。
希望这个回答能够对你有所帮助。




