ThinkPHP在Apache下运行很正常,在nginx下运行出错,提示找不到文件,这是一个nginx设置的问题,和ThinkPHP无关,由于nginx默认设置不处理ThinkPHP所使用的PATH_INFO,查看nginx日志即可看出。
解决方法一:修改ThinkPHP设置,不使用PATH_INFO
解决方法二(推荐):修改nginx设置,支持PATH_INFO
修改nginx.conf和fastcgi-params
将
location ~ \.php$ {
修改为
location ~ \.php/?.*$ {
将
fastcgi_param SCRIPT_FILENAME $document_root2$fastcgi_script_name;
修改为
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root2$fastcgi_script_name2;
将
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
修改为
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
完整的nginx虚拟主机区块配置如下:
server {
listen 80;
server_name www.amiku.cn amiku.cn;
root /www_amiku_cn;
include /www_amiku_cn/.htaccess;
index index.html index.htm index.php;
location ~ .*\.(php|php5)/?.*$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
set $path_info “”;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME /www_amiku_cn$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include fcgi.conf;
}
}
另外附贴修改过的.htaccess,以满足nginx的伪静态和apache服务器的规则不同。
#RewriteEngine on
#RewriteRule ^index.html$ index.php/Index/index
#RewriteRule ^([A-Z][a-z]+).html$ index.php/$1/index
#RewriteRule ^([A-Z][a-z]+)-([a-z]+).html$ index.php/$1/$2
#RewriteRule ^([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([0-9]+).html$ index.php/$1/$2/$3/$4
#RewriteRule ^([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([\x00-\xff]+)-([a-z]+)-([0-9]+)-([a-z]+)-([0-9]+).html$ index.php/$1/$2/$3/$4/$5/$6/$7/$8rewrite ^(.*)/index.html$ $1/index.php/Index/index;
rewrite ^(.*)/([A-Z][a-z]+).html$ $1/index.php/$2/index;
rewrite ^(.*)/([A-Z][a-z]+)-([a-z]+).html$ $1/index.php/$2/$3;
rewrite ^(.*)/([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([0-9]+).html$ $1/index.php/$2/$3/$4/$5;
rewrite ^(.*)/([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([\x00-\xff]+)-([a-z]+)-([0-9]+)-([a-z]+)-([0-9]+).html$ $1/index.php/$2/$3/$4/$5/$6/$7/$8/$9;
2011/06/28 14:01:09
Now I feel stpuid. That’s cleared it up for me
回复