首页主机资讯Debian系统中Nginx监控与报警设置

Debian系统中Nginx监控与报警设置

时间2025-10-20 11:12:03发布访客分类主机资讯浏览675
导读:Debian系统中Nginx监控与报警设置指南 一、基础监控准备:启用Nginx状态模块 要监控Nginx状态,需先启用其内置的stub_status模块,用于暴露基础连接数指标。 编辑Nginx配置文件(通常为/etc/nginx/ng...

Debian系统中Nginx监控与报警设置指南

一、基础监控准备:启用Nginx状态模块

要监控Nginx状态,需先启用其内置的stub_status模块,用于暴露基础连接数指标。

  1. 编辑Nginx配置文件(通常为/etc/nginx/nginx.conf或在/etc/nginx/sites-available/default中添加):
    server {
        
        listen 80;
        
        server_name localhost;
    
        location /nginx_status {
        
            stub_status on;
        
            access_log off;
        
            allow 127.0.0.1;
          # 仅允许本地访问,提升安全性
            deny all;
    
        }
    
    }
    
    
  2. 重启Nginx使配置生效:
    sudo systemctl restart nginx
    
  3. 验证状态页面:在本地浏览器访问http://localhost/nginx_status,将显示类似以下信息:
    Active connections: 3 
    server accepts handled requests
     100 100 200 
    Reading: 0 Writing: 1 Waiting: 2
    
    关键指标说明
    • Active connections:当前活跃连接数(含Reading/Writing/Waiting状态);
    • accepts:累计接受的连接数;
    • handled:累计成功处理的连接数;
    • requests:累计处理的请求数;
    • Reading/Writing/Waiting:分别表示正在读取请求头、发送响应、保持空闲的连接数。

二、使用Prometheus+Grafana实现实时监控与报警

1. 安装Prometheus(数据采集与存储)

Prometheus是一款开源监控系统,通过“拉取”模式收集指标。

  1. 下载并解压Prometheus:
    wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
    tar xvfz prometheus-2.30.3.linux-amd64.tar.gz
    cd prometheus-2.30.3.linux-amd64
    
  2. 配置prometheus.yml,添加Nginx Exporter抓取任务:
    scrape_configs:
      - job_name: 'nginx'
        static_configs:
          - targets: ['localhost:9113']  # Nginx Exporter的地址
    
  3. 启动Prometheus:
    ./prometheus --config.file=prometheus.yml
    
    访问http://localhost:9090可查看Prometheus Web界面。

2. 安装Nginx Exporter(指标采集器)

Nginx Exporter将stub_status的指标转换为Prometheus可识别的格式。

  1. 下载并运行Nginx Exporter:
    wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter-0.11.0.linux-amd64.tar.gz
    tar xvfz nginx-prometheus-exporter-0.11.0.linux-amd64.tar.gz
    cd nginx-prometheus-exporter-0.11.0.linux-amd64
    ./nginx-prometheus-exporter -nginx.scrape-uri=http://localhost/nginx_status
    
    默认监听9113端口,输出指标示例:
    # HELP nginx_http_requests_total Total number of HTTP requests
    # TYPE nginx_http_requests_total counter
    nginx_http_requests_total{
    status="200",method="GET",handler="/"}
         100
    

3. 安装Grafana(可视化与报警)

Grafana用于将Prometheus中的指标可视化,并设置报警规则。

  1. 安装Grafana:
    sudo apt update &
        &
         sudo apt install -y grafana
    sudo systemctl enable --now grafana-server
    
  2. 访问http://localhost:3000,使用默认账号admin/admin登录。
  3. 添加Prometheus数据源:
    • 进入ConfigurationData SourcesAdd data source → 选择Prometheus
    • 配置URL为http://localhost:9090,点击Save & Test(需显示“Data source is working”)。

4. 配置Grafana仪表盘

  1. 导入官方Nginx监控仪表盘:
    • 点击左侧+DashboardImport
    • 输入仪表盘ID(如12708,官方Nginx基础看板),点击Import
  2. 仪表盘将展示Active connectionsRequests per second5xx error rate等关键指标的实时趋势。

5. 设置Prometheus报警规则

编辑Prometheus的rules.yml文件(或在prometheus.yml中添加rule_files),添加以下规则:

groups:
  - name: nginx_alerts
    rules:
      - alert: High5xxErrorRate
        expr: sum(rate(nginx_http_requests_total{
status=~"5.."}
    [5m])) / sum(rate(nginx_http_requests_total[5m])) >
 0.01  # 5xx错误率超过1%
        for: 5m  # 持续5分钟触发
        labels:
          severity: critical
        annotations:
          summary: "Nginx 5xx错误率过高 (instance {
{
 $labels.instance }
}
)"
          description: "过去5分钟5xx错误占比 {
{
 $value }
}
    ,超过1%阈值"
      - alert: HighRequestRate
        expr: sum(rate(nginx_http_requests_total[1m])) by (instance) >
 1000  # 每秒请求数超过1000
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "Nginx请求率过高 (instance {
{
 $labels.instance }
}
)"
          description: "当前请求率 {
{
 $value }
}
    ,超过1000阈值"
  1. 在Prometheus中加载报警规则:
    编辑prometheus.yml,添加:
    rule_files:
      - "rules.yml"
    
    重启Prometheus使规则生效。

6. 配置Grafana报警通知

  1. 进入Grafana左侧AlertingNotification channels
  2. 点击New channel,配置通知方式(如Email、Slack):
    • Name:Email Alerts
    • Type:Email
    • Settings:填写SMTP服务器信息(如Gmail的SMTP地址、端口、用户名、密码);
    • Save & Test:测试通知是否发送成功。
  3. 为报警规则绑定通知渠道:
    进入AlertingAlert rules,找到已创建的规则(如High5xxErrorRate),点击EditNotifications,选择对应的Notification channel(如Email Alerts)。

三、日志监控与异常告警

1. 配置Nginx日志格式(便于分析)

编辑/etc/nginx/nginx.conf,添加结构化日志格式(如JSON):

http {

    log_format json_analytics escape=json '{
"time":"$time_iso8601","host":"$host","status":"$status","request_time":"$request_time","remote_addr":"$remote_addr","request":"$request"}
    ';
    
    access_log /var/log/nginx/access.log json_analytics;
    
    error_log /var/log/nginx/error.log;

}
    

重启Nginx使配置生效:

sudo systemctl restart nginx

日志字段说明

  • status:HTTP状态码(如200、500);
  • request_time:请求处理时间(秒);
  • remote_addr:客户端IP;
  • request:请求路径与方法(如GET /api/payment)。

2. 使用ngxtop实时分析日志

ngxtop是一款实时日志分析工具,可快速定位异常请求。

  1. 安装ngxtop:
    sudo apt install -y python3-pip
    pip3 install ngxtop
    
  2. 实时查看错误请求(5xx状态码):
    ngxtop -i 'status >
        = 500' print request_path status request_time
    
    输出示例:
    Running for 10 seconds, 123 records processed: 12.3 req/sec
    request_path       status  request_time
    /api/payment       500     1.23
    /api/user/create   502     0.45
    
  3. 监控慢请求(响应时间超过1秒):
    ngxtop -i 'request_time >
         1' top request_path request_time
    
    输出示例:
    Running for 10 seconds, 456 records processed: 45.6 req/sec
    request_path       request_time
    /api/upload        3.45
    /api/report        2.12
    

3. 使用Fail2Ban防止恶意攻击

Fail2Ban可监控Nginx日志,自动封禁频繁发起恶意请求的IP。

  1. 安装Fail2Ban:
    sudo apt update &
        &
         sudo apt install -y fail2ban
    
  2. 创建Nginx监控配置:
    /etc/fail2ban/jail.local中添加:
    [nginx-http-auth]
    enabled = true
    filter = nginx-http-auth
    action = iptables[name=HTTP, port=80, protocol=tcp]
    logpath = /var/log/nginx/error.log
    maxretry = 3  # 3次失败后封禁
    bantime = 3600  # 封禁1小时
    
  3. 重启Fail2Ban:
    sudo systemctl restart fail2ban
    
    查看封禁记录:
    sudo fail2ban-client status nginx-http-auth
    

四、告警优化策略

  1. 告警分级:根据业务影响程度设置severity(如criticalwarninginfo),避免重要告警被淹没;
  2. 阈值动态调整:根据业务高峰期(如双11)调整阈值(如将请求率阈值从1000提升至2000);
  3. 告警合并:对同一类告警(如同一IP的多次失败)进行合并,减少通知数量;
  4. 自动化响应:通过脚本实现常见问题的自动修复(如Nginx进程崩溃时自动重启):
    #!/bin/bash
    if ! pgrep nginx >
         /dev/null;
         then
        systemctl restart nginx
        echo "Nginx restarted at $(date)" >
        >
         /var/log/nginx_monitor.log
    fi
    
    将脚本添加到cron定时任务(如每分钟执行一次):
    * * * * * /path/to/script.sh
    

通过以上步骤,可在Debian系统中构建一套完整的Nginx监控与报警体系,实现对服务状态、性能指标、日志异常的实时监控,及时发现并解决问题,保障服务高可用性。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Debian系统中Nginx监控与报警设置
本文地址: https://pptw.com/jishu/729877.html
如何在Debian上实现Nginx自动备份 Debian下Nginx静态资源处理方法

游客 回复需填写必要信息