问题:服务器后台运行2个PHP框架,TP和SLIM,使用NGINX ENGINE运行,出现TP框架PHP请求可正常返回,SLIM框架PHP请求出现404错误
TP访问URL:index.php?m=xxx&c=xxx&a=xxx
SLIM访问URL:/xxx/xxx.xxx.php/abc
原始conf所设置LOCATION为
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
只支持TP框架的PHP访问。
考虑对/xxx/...路径增加额外的LOCATION规则,即
location ^~ /xxx{
}
由于NGINX在匹配LOCATION时会匹配的最长的LOCATION规则,所以在该location要放在原始location规则之前,且要加^~标识。
这里^~代表URL一旦匹配到/xxx规则,就不继续向下进行下面的LOCATION匹配。
在添加规则时绕了许多弯子,尝试在location中指定root或 alias,增加try_files uri等均不能解决问题,后来在
http://www.laozuo.org/6896.html
http://blog.haohtml.com/archives/5941
等网页上了解到slim这种PHP访问模式属于PATHINFO模式,NGINX默认不支持该访问模式,需要在LOCATION手工增加
SCRIPT_FILENAMESCRIPT_NAME PATH_INFO的信息,于是参考http://www.laozuo.org/6896.html更改LOCATION为:
location ^~ /xxx/{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { #正则匹配出.php和后面的path
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;#指定SCRIPT
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info; #指定PATH_INFO
}
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
问题解决