在 Debian 上监控 Nginx 可以从系统层、Nginx 自身状态、日志、第三方工具几个方面来做。下面按“由简到繁”的方式给你一个实用方案。
stub_status编辑 Nginx 配置文件(如 /etc/nginx/conf.d/status.conf):
server {
listen 127.0.0.1:8080;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx
访问:
curl http://127.0.0.1:8080/nginx_status
示例输出:
Active connections: 12
server accepts handled requests
1000 1000 3000
Reading: 1 Writing: 2 Waiting: 9
✅ 可监控:
systemdsystemctl status nginx
设置开机自启 & 异常重启:
systemctl enable nginx
可加守护(可选):
[Service]
Restart=on-failure
/var/log/nginx/access.log/var/log/nginx/error.log实时查看:
tail -f /var/log/nginx/error.log
统计异常:
grep "500" /var/log/nginx/access.log | wc -l
nginx-status + watchwatch -n 1 "curl -s http://127.0.0.1:8080/nginx_status"
htop / netstathtop
ss -ant | grep :80
适合服务器集群。
组件:
node_exporter:系统指标nginx-prometheus-exporter:Nginx 指标Prometheus + Grafana示例 exporter 启动:
nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:8080/nginx_status
Grafana 可直接看:
netdata一条命令安装:
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
打开浏览器:
http://服务器IP:19999
✅ 自动监控:
可配合:
fail2ban(防攻击)ZabbixPrometheus Alertmanager| 场景 | 推荐 |
|---|---|
| 个人 / VPS | stub_status + log + htop |
| 小公司 | netdata |
| 生产环境 | Prometheus + Grafana |
如果你愿意,我可以:
你现在是学习用途还是生产环境?