Nginx 1.20 运行 ThinkPHP 8.0 需配置 PATHINFO 兼容规则:location / { try_files $uri $uri/ /index.php?$query_string; },并确保 root 指向 public 目录、fastcgi_param SCRIPT_FILENAME 准确、禁用 autoindex,重启 Nginx 后可支持 /user/index 等伪静态访问。

在 Nginx 1.20 中运行 ThinkPHP 8.0,需正确配置 rewrite 规则以支持 PATHINFO 或兼容模式的伪静态访问(如 /index.php/user/index 或直接 /user/index)。TP8 默认使用 PATHINFO 模式,Nginx 本身不支持 .htaccess,必须在 server 块中显式配置。
基础 rewrite 规则(推荐:兼容 PATHINFO)
适用于 TP8 默认的 url_common' => true(即开启 PATHINFO)且入口文件为 public/index.php 的标准部署结构:
修复 Core bailout 状态损坏安全问题,以及 BZ2、DOM、PDO、Opcache/JIT 内存与执行错误,改进 Unicode 字素处理、URL 解析、TLS 流状态、信号分发及会话错误处理。
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # 或 unix:/var/run/php/php8.1-fpm.sock
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
说明:
– try_files 将所有非静态资源请求兜底到 index.php,并透传完整查询参数(含 PATHINFO 部分);
– ThinkPHP 8.0 会自动从 $_SERVER['REQUEST_URI'] 或 $_SERVER['QUERY_STRING'] 解析路由,无需额外重写 PATHINFO;
– 不需要 rewrite ^(.*)$ /index.php?s=$1; 这类旧版规则(TP5 常用),TP8 已优化解析逻辑。
若启用「兼容模式」(APP_DEBUG=false + url_html_suffix)
当配置了 'url_html_suffix' => 'html' 并关闭调试模式时,需确保带 .html 后缀的请求也能进 PHP:
- 确认
location ~ \.php$块已存在(上文已有); - 添加对 .html 后缀的转发(可选,仅当真有 .html 路由需求):
location ~ \.html$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param QUERY_STRING $query_string;
include fastcgi_params;
}
关键注意事项
-
root 必须指向 public 目录:例如
root /var/www/myapp/public;,否则index.php路径会出错; -
禁用 autoindex:避免目录被列出来,加
autoindex off;; -
不要遗漏 fastcgi_params:尤其是
SCRIPT_FILENAME必须准确,否则报 “No input file specified”; -
重启或重载 Nginx:修改后执行
nginx -t && nginx -s reload; -
检查 TP8 配置:确认
config/app.php中'url_common' => true,且'url_route_must' => false(按需)。
验证是否生效
访问 http://your-domain.com/index.php/route/param 应正常响应;
若配置了 'url_convert' => true 和路由定义,再访问 http://your-domain.com/route/param(无 index.php)也应成功 —— 表示伪静态已工作。


















