PHP-FPM status 页面若未限制访问,会暴露请求 URL 和资源信息,根据 2026 年 4 月 25 日的技术文档,必须通过 Nginx 的 fastcgi_pass 显式转发并配合 listen.allowed_clients = 127.0.0.1 限制访问来源。
原因分析
PHP-FPM status 页面返回 404 或 502 错误的根本原因不是 PHP-FPM 没开启 status 功能,而是 Nginx 未通过 fastcgi_pass 将请求转发至 PHP-FPM。根据 2026 年 4 月 25 日的配置文档,常见问题包括 location 未匹配、匹配后缺失 fastcgi_pass、SCRIPT_FILENAME 为空值、PATH_INFO 未设置、pm.status_path 路径不一致或访问控制限制。
status 页面会显示敏感信息包括:进程池名称 (pool)、进程管理器类型 (static/dynamic/ondemand)、启动时间 (start time) 以及请求 URL。根据 2023 年 4 月 21 日的官方说明,为安全起见,FPM status 页面应限制为内部请求或已知客户端 IP 访问。
解决方案
1. PHP-FPM 池配置启用 status 路径
在 PHP-FPM 配置文件 (通常是/etc/php/8.2/fpm/pool.d/www.conf) 中添加以下指令:
pm.status_path = /status
同时设置监听限制:
listen.allowed_clients = 127.0.0.1
根据 2020 年 10 月 14 日的 php-fpm 配置文档,listen.allowed_clients 默认值是 any,如果不设置则允许任何服务器请求连接,生产环境必须明确限制为 127.0.0.1。
2. Nginx location 块正确配置 fastcgi_pass
Nginx 配置必须显式声明 fastcgi_pass,指向 PHP-FPM 监听地址。根据 2026 年 4 月 25 日的配置示例:
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME "";
fastcgi_param PATH_INFO $fastcgi_script_name;
}
关键点:SCRIPT_FILENAME 必须设为固定空值,否则 PHP-FPM 会尝试加载不存在的脚本;PATH_INFO 必须传递请求路径,否则 PHP-FPM 无法识别是 status 还是 ping 接口。
3. 添加 IP 访问限制
在 Nginx 中进一步限制访问来源,只允许内网或特定 IP:
location ~ ^/status$ {
allow 127.0.0.1;
allow 10.0.0.0/8;
deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
根据 2023 年 4 月 21 日的文档,查询参数可控制输出格式:?json 返回 JSON 格式、?html 返回 HTML 格式、?full 返回完整输出,监控时应使用?json&full 获取完整数据。
4. 禁用危险函数与错误显示
在 php.ini 中设置以下安全参数,根据 2025 年 11 月 5 日的配置指南:
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
文件权限设置:PHP 文件设为 644,目录设为 755,敏感目录设为 700,命令示例:chmod 644 /var/www/html/*.php、chmod 700 /var/www/html/config。
注意事项
1. 常见错误:浏览器直接显示 404 或 502,实际是 Nginx 返回了 200 但内容为空,原因是 location 块里没配 fastcgi_pass,Nginx 当普通路径处理去查磁盘文件。
2. 路径一致性:Nginx 的 location 路径 (如/status) 必须和 PHP-FPM 对应 pool 里的 pm.status_path 完全一致,包括开头的/,否则无法匹配。
3. Unix Socket 权限:如果使用 unix socket 方式 (如 unix:/run/php/php8.2-fpm.sock),需确保 listen.owner = www、listen.group = www、listen.mode = 0666,根据 2020 年 10 月 14 日文档,否则会出现 502 Bad Gateway。
4. 监控工具集成:根据 2025 年 8 月 15 日的建议,可使用 Prometheus、Grafana 实时监控 PHP-FPM 性能,通过?json&openmetrics 格式输出便于采集。
5. 生产环境禁用:根据 2026 年 3 月 25 日的安全文档,生产环境建议完全关闭 status 页面或通过防火墙规则限制,避免信息泄露导致攻击者了解系统目录结构和资源使用情况。
参考来源
来源:PHP 官方文档 - Status Page 配置与安全限制说明 (2023 年 4 月 21 日发布)
来源:技术博客 - PHP-FPM 监控难题全解析 (2026 年 1 月 4 日发布)
来源:配置指南 - 为什么 PHP-FPM 的 status 页面无法通过浏览器访问 (2026 年 4 月 25 日撰)
来源:安全文档 - php 配置如何设置文件权限 (2025 年 11 月 5 日截至)