
在 Nginx + PHP-FPM 环境中,需通过 fastcgi_param PHP_VALUE 单行传递多个 include_path 目录(用分号分隔),否则后置参数会覆盖前置值,导致 PHP 仅识别最后一个路径。
在 nginx + php-fpm 环境中,需通过 fastcgi_param php_value 单行传递多个 include_path 目录(用分号分隔),否则后置参数会覆盖前置值,导致 php 仅识别最后一个路径。
当从 Apache 迁移至 Nginx 时,开发者常误以为可像 Apache 的 php_value include_path 那样逐行声明多个路径。但 Nginx 的 fastcgi_param 指令不具备累加性——重复使用同一名字的 fastcgi_param 会导致前序值被完全覆盖。正如你在配置中所写:
fastcgi_param PHP_VALUE "include_path=/sites/web-test1/vendor/webtoolkit/src/includes"; fastcgi_param PHP_VALUE "include_path=/sites/web-test1/vendor/"; fastcgi_param PHP_VALUE "include_path=/sites/web-test1/private/"; fastcgi_param PHP_VALUE "include_path=/usr/share/php/";
最终生效的只有最后一行:include_path=/usr/share/php/,因此 PHP 报错 Failed opening 'includes/emailpriv.inc.php' —— 因为它根本找不到 /sites/web-test1/private/includes/emailpriv.inc.php。
✅ 正确做法是:将所有路径合并为一条 PHP_VALUE 声明,各 include_path= 项以英文分号 ; 连接,并确保路径末尾不带多余斜杠(避免路径拼接异常):
location ~ \.(php|html|htm)$ {
try_files $uri =404;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# ✅ 关键修正:单行、分号分隔、路径规范
fastcgi_param PHP_VALUE "include_path=/sites/web-test1/vendor/webtoolkit/src:/sites/web-test1/vendor:/sites/web-test1/private:/usr/share/php";
}
⚠️ 注意事项:
-
分隔符必须是英文分号
;(非冒号:),且include_path=必须显式重复(Nginx 不支持:分隔的路径列表,那是open_basedir或 CLI 的语法); - 路径中不要以
/结尾(如/vendor/→ 改为/vendor),否则include('includes/emailpriv.inc.php')可能解析为/vendor//includes/emailpriv.inc.php,触发文件系统错误; - 若路径含空格或特殊字符,需用双引号包裹整个
PHP_VALUE字符串(当前已满足); -
PHP_VALUE仅对当前location生效;若需全局生效,可置于server或http块中(但需评估安全性风险); - 验证是否生效:在项目中创建
phpinfo.php,访问后搜索include_path,确认显示值与配置完全一致。
? 补充建议:
为提升可维护性,可将该配置抽离为独立文件(如 /etc/nginx/snippets/php-include-paths.conf),再通过 include 引入:
# /etc/nginx/snippets/php-include-paths.conf fastcgi_param PHP_VALUE "include_path=/sites/web-test1/vendor/webtoolkit/src:/sites/web-test1/vendor:/sites/web-test1/private:/usr/share/php";
并在 location 中调用:
location ~ \.(php|html|htm)$ {
# ... 其他配置
include snippets/php-include-paths.conf;
}
最后,重启服务使配置生效:
sudo nginx -t && sudo systemctl reload nginx sudo systemctl restart php7.4-fpm # 请按实际版本调整
完成配置后,<?php include('includes/emailpriv.inc.php'); ?> 将按顺序在四个目录中查找该文件,不再报错。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











