当前位置:首页 > PHP教程 > PHP高级教程

让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享

网上搜了很多方法都不奏效,研究了一天,发现通过以下的配置可以完美支持 '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下的请求都转发到index.php来处理,亦即thinkphp的单一入口文件;然后把对php文件的请求交给fastcgi来处理,并且添加对path_info的支持。
重启nginx以后,http://localhost/project/index/insert, http://localhost/project/index.php/index/delete 这样的url都可以正确访问了。
还有一个地方需要注意的是,nginx配置文件里 if 和后面的括号之间要有一个空格,不然会报unknown directive错误。
【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!