在 Debian 上“监控 Nginx”通常分三层:进程/服务是否活着、运行状态与性能、业务/访问日志与告警。下面按从简到繁给你一套可落地的做法。
systemctl status nginx
或
service nginx status
开机自启:
systemctl enable nginx
ps aux | grep nginx
ss -lntp | grep ':80\|:443'
编辑站点配置(如 /etc/nginx/sites-enabled/default):
server {
listen 80;
server_name localhost;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
测试并重载:
nginx -t
systemctl reload nginx
访问:
curl http://127.0.0.1/nginx_status
输出示例:
Active connections: 12
server accepts handled requests
1000 1000 2500
Reading: 0 Writing: 1 Waiting: 11
指标含义:
top -p $(pgrep nginx)
htop
ss -s
cat /proc/$(pgrep nginx | head -1)/limits
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
# 状态码统计
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
# top IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head
* * * * * curl -sf http://127.0.0.1/nginx_status || systemctl restart nginx
安装:
apt install monit
示例 /etc/monit/conf.d/nginx:
check process nginx with pidfile /run/nginx.pid
start program = "/usr/sbin/service nginx start"
stop program = "/usr/sbin/service nginx stop"
if failed host 127.0.0.1 port 80 protocol http then restart
systemctl restart monit
nginx-prometheus-exporterbash <(curl -Ss https://my-netdata.io/kickstart.sh)
浏览器访问 http://IP:19999
✅ 必须关注:
⚠️ 预警阈值示例:
如果你愿意,我可以:
你现在是个人服务器还是生产环境?