nginx开启状态监控需启用ngx_http_stub_status_module模块,在server或http块中配置location并设stub_status on。需确认模块已编译(nginx -v检查),限制访问权限,推荐配合auth_basic增强安全,最后nginx -t验证并reload生效。

在 Nginx 的 location 配置中开启状态监控,本质是启用 ngx_http_stub_status_module 模块提供的 /status(或自定义路径)接口。这个模块必须已编译进 Nginx,且需在 server 或 http 块内添加对应 location 块。
先确认模块是否可用
运行命令:
nginx -V 2>&1 | grep -o with-http_stub_status_module
如果有输出,说明模块已内置;若无,需重新编译 Nginx 并加入 --with-http_stub_status_module 参数。
标准 location 配置写法
在任意 server{...} 或 http{...} 块中添加:
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
-
stub_status on是核心指令,不可省略(旧版本可写stub_status;,新版本推荐显式写on) -
access_log off避免状态页访问刷爆日志 -
allow和deny控制访问权限,生产环境严禁开放deny all之外的宽泛 IP(如allow all)
支持远程查看的常见调整
比如只允许可信内网段访问:
location /status {
stub_status on;
access_log off;
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all;
}
配合基础认证增强安全性(推荐生产使用)
location /status {
stub_status on;
access_log off;
auth_basic "Nginx Status";
auth_basic_user_file /etc/nginx/.htpasswd;
allow 127.0.0.1;
allow 192.168.10.50;
deny all;
}
需提前用 htpasswd 工具生成账号文件,例如:
htpasswd -c /etc/nginx/.htpasswd monitor
验证配置并生效
nginx -t # 检查语法 nginx -s reload # 重载配置(不中断服务)
访问测试(本地):
curl http://127.0.0.1/nginx_status
正常返回类似:
Active connections: 3 server accepts handled requests 1234 1234 2468 Reading: 0 Writing: 1 Waiting: 2
注意:该状态页只能反映当前 worker 进程汇总数据,不支持按虚拟主机或 upstream 单独统计。











