phpEnv 的 Nginx 默认不启用 stub_status,需先确认内置 http_stub_status_module 模块(nginx -V | grep),再在 server 块内配置 location /status 并重载;否则返回 404 或 403。

phpEnv 环境下的 Nginx 默认不启用 stub_status,必须手动确认模块存在、修改配置并重载,否则访问 /status 会返回 404 或 403。
检查 phpEnv 的 Nginx 是否内置 http_stub_status_module
phpEnv 多数基于编译安装的 Nginx,但未必默认开启该模块。直接运行:
nginx -V 2>&1 | grep -o with-http_stub_status_module
如果输出空,说明模块未编译进当前 Nginx —— 此时无法仅靠改配置启用 stub_status,必须重新编译 Nginx 并加上 --with-http_stub_status_module 参数。常见于 phpEnv 旧版本或精简打包版。
- 若输出
with-http_stub_status_module,继续下一步 - 若没输出,别折腾配置文件了,先确认 phpEnv 是否支持模块热添加;不支持就只能换用已含该模块的 Nginx 版本(如 phpEnv 新版或官方预编译包)
在 phpEnv 的 nginx.conf 中添加 status location
phpEnv 的 Nginx 配置通常位于 /phpenv/nginx/conf/nginx.conf 或 /phpenv/nginx/conf/vhost/default.conf。不要加在 server 外层,必须嵌套在某个 server 块内(推荐加在 default server):
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
注意几个关键点:
-
stub_status on是必需指令,on不能省略或写成1 -
allow/deny顺序不能颠倒,否则所有请求都被拒绝 - 若需从宿主机外访问(比如 Zabbix 服务器),把
allow 127.0.0.1改成具体 IP 段,例如allow 192.168.1.0/24 - 不要在该 location 内加
root或index,stub_status 不依赖静态文件
验证配置并测试访问
执行以下命令验证语法并重载:
nginx -t && nginx -s reload
若提示 nginx: [emerg] unknown directive "stub_status",说明模块确实缺失;若提示 syntax is ok,再测试:
curl http://127.0.0.1/status
预期返回类似:
Active connections: 3 server accepts handled requests 123 123 456 Reading: 0 Writing: 1 Waiting: 2
如果返回 403,检查 allow/deny 规则是否屏蔽了你的请求来源;如果返回 404,确认 location 路径是否拼错(比如写成 /nginx_status 却用 /status 访问),或是否加在了错误的 server 块中(比如被注释、或不在监听 80 的 server 内)。
用脚本提取指标时注意字段位置固定性
stub_status 输出是纯文本、无 HTML 标签,但字段顺序和空格数量严格固定。例如第一行总是 Active connections: N,第二行是固定字符串 server accepts handled requests,第三行才是三个数字。解析脚本(如用于 Zabbix 的 nginx_status.sh)不能依赖正则模糊匹配,应按行+空格分割提取:
- 第 1 行:取冒号后整数 →
Active connections - 第 3 行:按空格切分,第 1 个是
accepts,第 2 个是handled,第 3 个是requests - 第 4 行:
Reading:后数字 →Reading;Writing:后 →Writing;Waiting:后 →Waiting
任何一行格式变动(比如 Nginx 升级后加了新字段)都会导致脚本解析失败 —— 这是实际运维中最容易忽略的兼容性断点。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











