ThinkPHP 8.0 部署在二级目录需三处对齐:独立 location /youdomain/ 块(末尾斜杠不可省)、root 指向父目录、fastcgi_param 显式指定子目录入口路径;rewrite 用 try_files + /youdomain/public/index.php?$query_string;框架 config/app.php 保持 url_route_on=true,不锁死域名。

ThinkPHP 8.0 部署在二级目录(如 https://example.com/youdomain/)时,Nginx 伪静态不能套用根目录规则,必须显式识别子目录路径、正确转发请求,并确保框架能解析出真实路由。核心是三处对齐:location 匹配前缀、root 指向物理根、fastcgi_param 透传完整上下文。
必须用独立 location /youdomain/ 块
不能把规则写在 location / { } 里,也不能用 if (!-e $request_filename) 做兜底。必须单独声明子目录级 location:
-
location /youdomain/ { }—— 注意末尾斜杠不能省,否则/youdomain(无斜杠)请求不匹配 - 该块需与
location / { }并列存在,不可嵌套 - 入口文件若在
youdomain/public/index.php,则此 location 内所有路径操作都基于/youdomain/上下文
rewrite 规则要带子目录前缀并传参
在 location /youdomain/ { } 块中,使用 try_files 更安全(避免 Windows 下 PATH_INFO 清空):
修复 Core bailout 状态损坏安全问题,以及 BZ2、DOM、PDO、Opcache/JIT 内存与执行错误,改进 Unicode 字素处理、URL 解析、TLS 流状态、信号分发及会话错误处理。
try_files $uri $uri/ /youdomain/public/index.php?$query_string;- 注意:fallback 路径是
/youdomain/public/index.php,不是/index.php - 必须用
$query_string,TP8 默认走 querystring 模式,不依赖 PATH_INFO
root 和 PHP 处理块要同步适配
假设项目完整路径为 /www/wwwroot/example.com/youdomain/,且入口在 youdomain/public/index.php:
立即学习“PHP免费学习笔记(深入)”;
-
root /www/wwwroot/example.com;—— root 指向项目父目录(即 youdomain 所在目录) - 在
location ~ \.php$ { }中,必须写:fastcgi_param SCRIPT_FILENAME $realpath_root/youdomain/public/index.php;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root/youdomain/public$fastcgi_path_info;fastcgi_split_path_info ^(.+\.php)(/.+)$;
框架内确认关键配置
仅改 Nginx 不够,TP8 必须“认得”这个二级环境:
-
config/app.php中:'url_route_on' => true -
'pathinfo_depr' => '/'(保持默认即可) - 不要设
app_host或domain_bind锁死域名,除非你明确需要 - 访问
https://example.com/youdomain/index.php/index/test能通,再试/youdomain/index/test—— 成功才说明真生效


















