phpStudy V8.1中Nginx子目录部署必须用独立location /subdir/块,不可在根location /内写rewrite;需配if+rewrite或try_files,root与运行目录严格一致,且ThinkPHP须关闭url_html_suffix、设pathinfo_depr为'/'。

phpStudy V8.1 中 Nginx 虚拟目录必须用 location /subdir/ 显式声明
直接在根 location / 里写 rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1 last; 是无效的——Nginx 不会自动把请求路径映射到子目录物理路径,必须用独立 location 块承接。否则所有子目录请求都会被根规则拦截,s= 参数为空,ThinkPHP 解析出错,返回 404 或白屏。
正确做法是:在 server 块内、location / 之前或之后(顺序不影响),添加专属块:
location /myapp/ {
if (!-e $request_filename) {
rewrite ^/myapp/(.*)$ /myapp/index.php?s=$1 last;
}
}
注意三点:
-
/myapp/末尾斜杠不能省,否则/myapp(无尾斜杠)请求不匹配 -
root指向的是整个站点根目录(如D:/www),不是myapp子目录;index.php必须真实存在于D:/www/myapp/index.php - 若项目是 ThinkPHP 6/8,且入口文件在
myapp/public/index.php,则需把root改为D:/www/myapp/public,并改写为rewrite ^/myapp/(.*)$ /index.php?s=$1 last;
ThinkPHP 在子目录下必须关掉 url_html_suffix 并设 pathinfo_depr 为 '/'
子目录部署时,框架仍按根路径解析路由,url_html_suffix 若非空(比如设成 'html'),会导致 /myapp/user/list 被强制重写为 /myapp/user/list.html,而该路径实际不存在,直接 404。
立即学习“PHP免费学习笔记(深入)”;
同时,pathinfo_depr 必须与 Nginx 重写中 s= 的分隔逻辑一致:
- Nginx 规则用
s=/user/list→ PHP 层要能从$_SERVER['PATH_INFO']提取/user/list,所以pathinfo_depr得是'/' - 若误设为
'-',框架会尝试拆/user/list成user-list,路由完全失配 -
url_route_on必须为true,否则整个路由系统不启动,所有请求走默认控制器
if (!-e $request_filename) 在 phpStudy V8.1 + Windows 下仍有兼容风险
phpStudy V8.1 默认使用 Nginx 1.22.x(Windows 版),其 if 指令在嵌套或高并发下偶发 PATH_INFO 清空,导致 ThinkPHP 8.0+ 白屏(响应 200 但无输出)。这不是配置错误,而是 Windows 下 Nginx 的已知行为。
更稳的替代方案是改用 try_files,但必须配合 $realpath_root:
location /myapp/ {
try_files $uri $uri/ /myapp/index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
关键点:
-
$realpath_root而非$document_root,否则符号链接或路径拼接失败,autoload 找不到类 -
try_files后的/myapp/index.php必须和物理路径严格对应,不能漏/myapp/前缀 - 此写法要求入口文件
index.php在子目录下,且框架支持 QUERY_STRING 模式(TP6/8 默认支持)
验证前必须确认 root 和运行目录指向同一级物理路径
小皮面板里「网站」→「管理」→「基本设置」中的「运行目录」,和 Nginx 配置里的 root 值,必须指向同一个文件夹。常见错误是:
- 「运行目录」填了
D:/www/myapp,但 Nginxroot写成D:/www→ PHP 找不到index.php,报Primary script unknown - 「运行目录」填了
D:/www/myapp/public,但 Nginxroot还是D:/www/myapp→$realpath_root拼错,autoload 失败,白屏不报错 - 没重启 Nginx:改完配置后必须点 phpStudy 界面右上角「重启」按钮,仅刷新页面无效
最保险的验证顺序:http://localhost/myapp/index.php?r=index/hello(能显示)→ http://localhost/myapp/index/hello(应显示同内容)→ 查 error.log 里有没有 Primary script unknown 或 no input file specified。



















