Debian系统中Nginx监控与报警设置
导读:Debian系统中Nginx监控与报警设置指南 一、基础监控准备:启用Nginx状态模块 要监控Nginx状态,需先启用其内置的stub_status模块,用于暴露基础连接数指标。 编辑Nginx配置文件(通常为/etc/nginx/ng...
Debian系统中Nginx监控与报警设置指南
一、基础监控准备:启用Nginx状态模块
要监控Nginx状态,需先启用其内置的stub_status
模块,用于暴露基础连接数指标。
- 编辑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; } }
- 重启Nginx使配置生效:
sudo systemctl restart nginx
- 验证状态页面:在本地浏览器访问
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是一款开源监控系统,通过“拉取”模式收集指标。
- 下载并解压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
- 配置
prometheus.yml
,添加Nginx Exporter抓取任务:scrape_configs: - job_name: 'nginx' static_configs: - targets: ['localhost:9113'] # Nginx Exporter的地址
- 启动Prometheus:
访问./prometheus --config.file=prometheus.yml
http://localhost:9090
可查看Prometheus Web界面。
2. 安装Nginx Exporter(指标采集器)
Nginx Exporter将stub_status
的指标转换为Prometheus可识别的格式。
- 下载并运行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中的指标可视化,并设置报警规则。
- 安装Grafana:
sudo apt update & & sudo apt install -y grafana sudo systemctl enable --now grafana-server
- 访问
http://localhost:3000
,使用默认账号admin
/admin
登录。 - 添加Prometheus数据源:
- 进入
Configuration
→Data Sources
→Add data source
→ 选择Prometheus
; - 配置URL为
http://localhost:9090
,点击Save & Test
(需显示“Data source is working”)。
- 进入
4. 配置Grafana仪表盘
- 导入官方Nginx监控仪表盘:
- 点击左侧
+
→Dashboard
→Import
; - 输入仪表盘ID(如
12708
,官方Nginx基础看板),点击Import
。
- 点击左侧
- 仪表盘将展示
Active connections
、Requests per second
、5xx 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阈值"
- 在Prometheus中加载报警规则:
编辑prometheus.yml
,添加:
重启Prometheus使规则生效。rule_files: - "rules.yml"
6. 配置Grafana报警通知
- 进入Grafana左侧
Alerting
→Notification channels
; - 点击
New channel
,配置通知方式(如Email、Slack):- Name:
Email Alerts
; - Type:
Email
; - Settings:填写SMTP服务器信息(如Gmail的SMTP地址、端口、用户名、密码);
- Save & Test:测试通知是否发送成功。
- Name:
- 为报警规则绑定通知渠道:
进入Alerting
→Alert rules
,找到已创建的规则(如High5xxErrorRate
),点击Edit
→Notifications
,选择对应的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是一款实时日志分析工具,可快速定位异常请求。
- 安装ngxtop:
sudo apt install -y python3-pip pip3 install ngxtop
- 实时查看错误请求(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
- 监控慢请求(响应时间超过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。
- 安装Fail2Ban:
sudo apt update & & sudo apt install -y fail2ban
- 创建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小时
- 重启Fail2Ban:
查看封禁记录:sudo systemctl restart fail2ban
sudo fail2ban-client status nginx-http-auth
四、告警优化策略
- 告警分级:根据业务影响程度设置
severity
(如critical
、warning
、info
),避免重要告警被淹没; - 阈值动态调整:根据业务高峰期(如双11)调整阈值(如将请求率阈值从1000提升至2000);
- 告警合并:对同一类告警(如同一IP的多次失败)进行合并,减少通知数量;
- 自动化响应:通过脚本实现常见问题的自动修复(如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