温馨提示×

怎样监控debian nginx

小樊
46
2026-09-24 20:44:32
栏目: 智能运维

在 Debian 上监控 Nginx 可以从系统层、Nginx 自身状态、日志、第三方工具几个方面来做。下面按“由简到繁”的方式给你一个实用方案。


一、最基础:Nginx 自带状态监控(推荐先做)

1. 启用 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

✅ 可监控:

  • 活跃连接数
  • 请求量
  • 等待连接数

二、系统级监控(必须)

2. 使用 systemd

systemctl status nginx

设置开机自启 & 异常重启:

systemctl enable nginx

可加守护(可选):

[Service]
Restart=on-failure

三、日志监控(非常重要)

3. Nginx 日志位置

  • 访问日志:/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

四、命令行轻量监控工具

4. nginx-status + watch

watch -n 1 "curl -s http://127.0.0.1:8080/nginx_status"

5. htop / netstat

htop
ss -ant | grep :80

五、专业监控方案(生产推荐)

6. Prometheus + Node Exporter + Nginx Exporter

适合服务器集群。

组件:

  • node_exporter:系统指标
  • nginx-prometheus-exporter:Nginx 指标
  • Prometheus + Grafana

示例 exporter 启动:

nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:8080/nginx_status

Grafana 可直接看:

  • QPS
  • 连接数
  • 5xx 错误率

六、最省事方案(新手友好)

7. 使用 netdata

一条命令安装:

bash <(curl -Ss https://my-netdata.io/kickstart.sh)

打开浏览器:

http://服务器IP:19999

✅ 自动监控:

  • Nginx
  • CPU / 内存
  • 网络
  • 磁盘 IO

七、告警(进阶)

可配合:

  • fail2ban(防攻击)
  • Zabbix
  • Prometheus Alertmanager

推荐组合(实用)

场景 推荐
个人 / VPS stub_status + log + htop
小公司 netdata
生产环境 Prometheus + Grafana

如果你愿意,我可以:

  • 给你 完整 Prometheus + Grafana 配置
  • 或针对 Debian 12 / Nginx 1.25 写一份可直接用的配置

你现在是学习用途还是生产环境?

0 踩