Nginx开启状态页面监控需启用http_stub_status_module模块并配置location /status;验证模块存在、添加allow/deny访问控制、重载配置后,可通过curl访问获取Active connections、accepts、handled、requests及Reading/Writing/Waiting等实时连接指标。

在 Nginx 中开启状态页面监控实时连接,核心是启用 http_stub_status_module 模块并配置一个专用的 location 路径。整个过程不依赖外部工具,纯靠 Nginx 自带能力,配置轻量、响应快。
确认 stub_status 模块已编译启用
该模块默认不启用,需先验证是否存在:
- 运行
/usr/local/nginx/sbin/nginx -V(注意大写 V) - 在输出中查找
--with-http_stub_status_module - 若未找到,说明编译时未包含该模块,需重新编译 Nginx 并添加
--with-http_stub_status_module参数
在 server 块中添加 status location 配置
编辑 nginx.conf,在任一 server{...} 块内(如默认的 80 端口 server)插入以下内容:
location /status {
stub_status on;
access_log off;
allow 127.0.0.1; # 仅允许本地访问
deny all; # 拒绝其他所有 IP
}说明:
-
stub_status on是启用状态页的必需指令 -
access_log off避免频繁刷新产生大量日志 -
allow/deny控制访问权限,生产环境切勿直接写allow all;如需远程查看,可替换为具体管理 IP,例如allow 192.168.1.100;
重载配置并验证页面可访问
保存配置后执行:
-
/usr/local/nginx/sbin/nginx -t检查语法是否正确 -
/usr/local/nginx/sbin/nginx -s reload重载生效 - 用 curl 或浏览器访问
http://localhost/status(或对应服务器 IP)
正常返回类似:
Active connections: 3 server accepts handled requests 1245 1245 2891 Reading: 0 Writing: 1 Waiting: 2
理解关键指标含义
返回的每行都有明确语义:
- Active connections:当前全部活跃连接数(包括 reading/writing/waiting)
- accepts:Nginx 启动后接受的 TCP 连接总数
- handled:成功处理完成的连接数(应 ≈ accepts,差值过大可能 worker 异常)
- requests:HTTP 请求总数(一个连接可发起多个请求)
- Reading:正在读取客户端请求头/体的连接数
- Writing:正在向客户端发送响应的连接数
- Waiting:keep-alive 等待新请求的空闲连接数(= Active − Reading − Writing)


















