Nginx配置ThinkPHP URL重写与PATHINFO支持详解
此前尝试多种网络方案均未成功,经过一天深入研究与测试,确认采用下述配置方案可完美适配 ThinkPHP 中 'URL_MODEL' 设置为 2 的运行环境。
具体配置代码如下:
location /project/ {
index index.php;
if (!-e $request_filename) {rewrite ^/project/(.*)$ /project/index.php/$1 last;
break;
}
}location ~ .+\.php($|/) {
set $script $uri;
set $path_info "/";
if ($uri ~ "^(.+\.php)(/.+)") {set $script $1;
set $path_info $2;
}fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php?IF_REWRITE=1;
include /APMServ/nginx/conf/fastcgi_params;fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root/$script;
fastcgi_param SCRIPT_NAME $script;
}该配置首先将 /project 目录下的所有请求统一转发至 ThinkPHP 的单一入口文件 index.php 进行处理;随后将对 PHP 文件的请求交由 fastcgi 模块处理,并通过相关参数配置实现对 PATHINFO 模式的完整支持。
完成 Nginx 重启后,类似 http://localhost/project/Index/insert 以及 http://localhost/project/index.php/Index/delete 这类包含路径信息的 URL 均可被正确解析和访问。
需特别注意,在 Nginx 配置文件中,if 关键字与其后的左括号之间必须保留一个空格,否则服务器将无法识别指令并抛出 unknown directive 错误。