ThinkPHP支 持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可。在Apache下只需要开启mod_rewrite模块就可以正常访问了,但是Nginx中默认是不支持PATHINFO的,所以我们需要修改 nginx.conf文件。
网上搜了很多方法都不奏效,研究了一天,发现通过以下的配置可以完美支持 '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错误。
最主要的来啦,因为以上的都是我复制的
下面的 是我的 配置新建一个conf文件,名称比如linlook.conf
内容:
location ~ .+\.PHP($|/) {
set $script $uri;
set $path_info "/";
include fcgi.conf
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 /www/wdlinux/nginx-0.8.54/conf/fastcgi_params;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root/$script;
fastcgi_param SCRIPT_NAME $script;
}
内容:
server {
listen 80;
server_name test.linlook.com;
root /www/web/test;
index index.PHP index.HTML index.htm;
include linlook.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(JS|css)?$ {
expires 12h;
}
}