Home  >  Article  >  Backend Development  >  配备Nginx支持ThinkPHP的URL重写和PATHINFO

配备Nginx支持ThinkPHP的URL重写和PATHINFO

WBOY
WBOYOriginal
2016-06-13 10:56:27710browse

配置Nginx支持ThinkPHP的URL重写和PATHINFO

ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可。在Apache下只需要开启mod_rewrite模块就可以正常访问了,但是Nginx中默认是不支持PATHINFO的,所以我们需要修改nginx.conf文件。

网上搜了很多方法都不奏效,研究了一天,发现通过以下的配置可以完美支持 'URL_MODEL' => 2 的情况了01 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错误。这个害我调了很久

?

1 楼 wpeng1123 2011-04-10  
我的项目文件是test ThinkPHP放在test下面 我这样改动之后  访问登录页面,它跳转到index.php/Public/login  但是却发生系统错误

系统发生错误
您可以选择 [ 重试 ] [ 返回 ] 或者 [ 回到首页 ]
[ 错误信息 ]

无法加载模块Public

求解

2 楼 Jocson 2011-11-22  
一样啊。。。。

3 楼 mengdejun 2011-11-22  
路径配置不正确。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn