Nginx自带一个状态模块ngx_http_stub_status_module
,可以显示基本的实时状态信息。
首先确认你的Nginx编译时包含了该模块:
nginx -V 2>&1 | grep -o with-http_stub_status_module
在Nginx配置文件中添加状态监控位置(通常在/etc/nginx/nginx.conf
或站点配置文件中):
server {
listen 80;
server_name yourdomain.com;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1; # 只允许本地访问
allow 192.168.1.0/24; # 或者允许你的内网IP段
deny all; # 拒绝其他所有IP
}
}
重新加载Nginx配置:
nginx -s reload
访问http://yourdomain.com/nginx_status
,你会看到类似如下的输出:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
安装Nginx Prometheus exporter:
docker run -d -p 9113:9113 nginx/nginx-prometheus-exporter -nginx.scrape-uri=http://<nginx-ip>/nginx_status
配置Prometheus抓取exporter数据
在Grafana中导入Nginx仪表板(如ID 12708)
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --real-time-html --log-format=COMBINED
Netdata提供开箱即用的Nginx监控:
# 安装Netdata
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
如果需要更多指标,可以考虑编译Nginx时加入ngx_http_status_module
或使用商业版的Nginx Plus,它提供了更丰富的监控指标和API。
通过以上配置,你可以实时监控Nginx的运行状态,及时发现性能瓶颈和异常情况。