Nginx 支持 http_stub_status_module 必须在编译时启用,无法动态加载;通过 nginx -V | grep with-http_stub_status_module 检查是否已内置,若无则需源码编译并添加 --with-http_stub_status_module 参数,再配置 location /nginx_status { stub_status; allow 127.0.0.1; deny all; } 并重载生效。

要让 Nginx 支持 http_stub_status_module,关键不是“安装后配置”,而是**编译时必须启用该模块**——它不能动态加载,也不能靠配置文件开启。
确认当前 Nginx 是否已含 stub_status 模块
运行以下命令:
nginx -V 2>&1 | grep -o with-http_stub_status_module如果输出为空,说明模块未编译进当前 Nginx,必须重新编译;有输出则可跳到配置步骤。
若模块缺失:源码编译并启用 stub_status
适用于自定义安装、精简镜像或旧版本场景。步骤如下:
Linux 安全审计工具,检查 SSH 配置、开放/监听端口、防火墙规则(ufw/iptables/nftables)、失败登录尝试、sudoers 权限...
- 安装基础依赖(CentOS/RHEL):
sudo yum install -y gcc pcre-devel zlib-devel openssl-devel - 下载源码(以 1.28.0 为例):
wget http://nginx.org/download/nginx-1.28.0.tar.gz && tar -zxvf nginx-1.28.0.tar.gz && cd nginx-1.28.0 - 配置编译参数,明确加入 stub_status:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module - 编译并安装:
make && sudo make install
配置 stub_status 状态页
模块就绪后,在 /usr/local/nginx/conf/nginx.conf 的某个 server 块内添加 location:
stub_status;
allow 127.0.0.1;
deny all;
}
注意:
• stub_status 只允许出现在 server 或 location 块中,不可放在 http 或 upstream 中
• 访问路径如 http://localhost/nginx_status,返回纯文本状态信息(Active connections、accepts、handled、requests 等)
• 如需远程访问,把 allow 127.0.0.1 替换为可信 IP 或子网,避免暴露敏感指标
验证与生效
执行三步检查:
- 语法检查:/usr/local/nginx/sbin/nginx -t
- 重载配置:/usr/local/nginx/sbin/nginx -s reload
- 测试访问:curl http://127.0.0.1/nginx_status,应返回类似:
Active connections: 1
server accepts handled request
123456 123456 234567
Reading: 0 Writing: 1 Waiting: 0

















