Prometheus监控nginx

创建nginx-docker配置文件

bash 复制代码
mkdir /data/nginx/conf.d/ -p
cd /data/nginx/conf.d/
data/nginx/conf.d# ls



nginx/conf.d配置文件

cat >>server.conf<< 'EOF'
server {
    listen 80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
EOF

nginx-docker

mkdir /data/docker-compose -p
cd /data/docker-compose/


cat >docker-compose.yaml<<'EOF'
version: '3'
services:
  nginx:
    image: nginx:1.21.6
    container_name: nginx
    restart: always
    volumes:
      - /data/nginx/conf.d:/etc/nginx/conf.d
      - /data/nginx/html:/usr/share/nginx/html
      - /data/nginx/log:/var/log/nginx
    ports:
      - 80:80
EOF

启动nginx_docker

docker-compose up -d

nginx 开启stub_status

bash 复制代码
确认是否有该模块 with-http_stub_status_module 模块

docker exec -it nginx nginx -V 2>&1 | grep -o with-http_stub_status_module
with-http_stub_status_module

nginx 开启stub_status配置
```
cat >>server.conf<< 'EOF'
server {
    listen 80;
    server_name localhost;
......
    
    location /stub_status {
        stub_status on;
        access_log off;
        allow 0.0.0.0/0;
        deny all;
    }
......
}
EOF


具体配置
```
server {
    listen 80;
    server_name localhost;

    location /stub_status {
        stub_status on;
        access_log off;
        allow 0.0.0.0/0;
        deny all;
    }
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

重新加载nginx 配置测试

bash 复制代码
root@prometheus220:/data/nginx/conf.d# docker exec -it nginx nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful



root@prometheus220:/data/nginx/conf.d# docker exec -it nginx nginx -s reload

root@prometheus220:/data/nginx/conf.d# curl http://localhost/stub_status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 
```

配置nginx-exporter

bash 复制代码
cd /data/nginx/

cat >docker-compose.yaml <<'EOF'
version: '3.3'
services:
  nginx_exporter:
    image: nginx/nginx-prometheus-exporter:0.8.0
    container_name: nginx_exporter
    hostname: nginx_exporter
    command:
      - '-nginx.scrape-uri=http://10.19.1.220/stub_status'
    restart: always
    ports:
      - '9113:9113'
EOF

启动

docker-compose up -d

确认docker ps -a

bash 复制代码
http://10.19.1.220:9113/metrics

prometheus.yml 增加配置

bash 复制代码
nginx_exporter监控样本数据

cat >> prometheus/prometheus.yml << 'EOF'
  - job_name: 'nginx_exporter'
    scrape_interval: 30s
    static_configs:
    - targets: ['10.19.1.220:9113']
      labels:
        instance: nginx服务器 
EOF

curl -X POST http://localhost:9090/-/reload

prometheus 界面显示nginx_exporter

增加告警阀值

bash 复制代码
cd /data/docker-prometheus

cat >>prometheus/alert.yml << 'EOF'
- name: nginx
  rules:
  #对任何实例操过30s无法联系的情况发出告警
  - alert: NginxDown
    expr: nginx_up == 0
    for: 30s
    labels: 
      serverity: critical
    annotations:
      summary: "nginx异常,实例:{{ $labels.instance }}"
      description: "{{ $labels.job }} nginx 已关闭"
EOF
```
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml
curl -X POST http://localhost:9090/-/reload

添加nginx_dashboard

bash 复制代码
https://grafana.com/grafana/dashboards/12708
相关推荐
我的运维人生6 分钟前
Nginx:高性能Web服务器与反向代理的深度解析
服务器·前端·nginx·运维开发·技术共享
lldhsds6 分钟前
借助keepalived配置高可用nginx集群
nginx·keepalived·高可用
数据安全小盾21 分钟前
2024年10款好用的图纸加密软件推荐|图纸加密用哪些加密软件?
大数据·运维·服务器·网络·web安全
都先生29 分钟前
Linux FTP服务问题排查
linux·运维·服务器
Amor风信子36 分钟前
【简单记录】Linux系统安装ZooKeeper
linux·运维·zookeeper
超维机器人1 小时前
强化雨天巡检,智能巡检机器人确保变电站安全运行
运维·人工智能·安全·机器人
misakivv2 小时前
Centos7.9 使用 Kubeadm 自动化部署 K8S 集群(一个脚本)
运维·云原生·容器·kubernetes·自动化
魏大橙3 小时前
长亭WAF绕过测试
android·运维·服务器
小小不董4 小时前
Oracle DB运维常用的视图及数据字典
运维·数据库·oracle·dba
中国云报4 小时前
黎巴嫩BP机爆炸事件启示录:我国应加快供应链安全立法
运维·网络·安全