1. stub_status模块(基础性能监控)
Nginx内置的stub_status模块可提供简单的性能指标(如活跃连接数、请求数、请求处理速率)。配置步骤:
/etc/nginx/sites-available/default)的server块中添加:location /stub_status {
stub_status on;
access_log off;
allow 127.0.0.1; # 仅允许本地访问
deny all;
}
sudo systemctl restart nginx。http://服务器IP/stub_status查看状态(需替换为实际IP)。1. 安装与配置Nginx Exporter
Nginx Exporter是将Nginx指标转换为Prometheus可识别格式的工具。步骤:
wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter_0.11.0_linux_amd64.deb
sudo dpkg -i nginx-prometheus-exporter_0.11.0_linux_amd64.deb
9113端口):sudo systemctl start nginx-prometheus-exporter
2. 配置Prometheus抓取指标
编辑Prometheus配置文件(/etc/prometheus/prometheus.yml),添加Nginx抓取任务:
scrape_configs:
- job_name: 'nginx'
scrape_interval: 10s # 抓取间隔
static_configs:
- targets: ['localhost:9113'] # Exporter地址
重启Prometheus:sudo systemctl restart prometheus。
3. Grafana可视化与报警
sudo apt install grafana,启动后访问http://localhost:3000(默认账号admin/admin)。Configuration > Data Sources,选择Prometheus并配置URL为http://localhost:9090。+ > Import,搜索“Nginx”并导入官方仪表盘(如ID 6686)。Alerting > Alert rules,点击New alert rule,选择Nginx指标(如nginx_http_requests_total、nginx_http_up),设置阈值(如请求量超过1000次/分钟触发报警),并配置通知渠道(邮件、Slack等)。1. 配置Nginx日志格式
在/etc/nginx/nginx.conf中定义结构化日志格式(便于后续分析):
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn; # 错误日志级别设为warn
}
重启Nginx使配置生效。
2. 常用日志分析工具
# 统计404错误数量
awk '$9 == 404 {count++} END {print count}' /var/log/nginx/access.log
# 实时监控错误日志
tail -f /var/log/nginx/error.log | grep -i "error\|fail"
sudo apt install goaccess,运行:goaccess /var/log/nginx/access.log -o report.html --log-format=COMBINED
打开report.html查看实时统计(包括错误码、访问来源等)。3. 自动化报警脚本
编写Shell脚本定期检查日志并发送告警(如邮件)。示例:
#!/bin/bash
ERROR_LOG="/var/log/nginx/error.log"
ALERT_THRESHOLD=5 # 5分钟内超过5次错误触发报警
TIME_WINDOW=$(date -d "5 minutes ago" +"%Y-%m-%d %H:%M:%S")
ERROR_COUNT=$(grep -c "$TIME_WINDOW.*error" "$ERROR_LOG")
if [ "$ERROR_COUNT" -gt "$ALERT_THRESHOLD" ]; then
echo "Nginx错误日志异常:$ERROR_COUNT次错误(时间窗口:$TIME_WINDOW)" | mail -s "Nginx报警" admin@example.com
fi
添加定时任务(每5分钟执行一次):
crontab -e
# 添加以下行
*/5 * * * * /path/to/alert_script.sh
4. 第三方监控工具
sudo apt install fail2ban
编辑/etc/fail2ban/jail.local,添加Nginx规则:[nginx]
enabled = true
port = http
logpath = /var/log/nginx/access.log
maxretry = 3 # 3次失败后封禁
bantime = 600 # 封禁10分钟
重启Fail2Ban:sudo systemctl restart fail2ban。nginx_http_requests_total)、活跃连接数(nginx_http_connections_active)、请求延迟(nginx_http_request_duration_seconds)。nginx_http_requests_total{status=~"4..|5.."})、错误日志数量(通过日志分析工具统计)。top -p $(cat /var/run/nginx.pid))、内存占用(htop)。以上方法覆盖了Debian Nginx从基础性能到高级日志、报警的全链路监控需求,可根据实际场景选择合适的工具组合。