近日有个需求,要nginx支持pathinfo模式,就是可以访问到aa.com/index.php/aa/xx/cc.html这种的链接,nginx默认不支持pathinfo模式,所以需要修改配置。
修改nginx的匹配php 的配置,原来的location是location ~ .*\.(php|php5)?$ 这样的url匹配到以.php结尾就不看后续路径了,
比如:aa.com/index.php/aa/xx/cc.html 就会认为index.php是目录了,如果是以index.php结尾的话就是处理php文件了,所以我们要将配置文件改成下面这样,这样的话既可以解析php文件,又能执行aa.com/index.php/aa/xx/cc.html这样而不会把index.php当成目录
location ~ \.php/?.*$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
#定义变量,fastcgi_script_name2,值为脚本名
set $fastcgi_script_name2 $fastcgi_script_name;
#判断脚本名是否包含.php
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
#是包含的情况下把.php的全名,例如index.php赋值变量fastcgi_script_name2
set $fastcgi_script_name2 $1;
#然后把index.php后面的内容赋值给path_info
set $path_info $2;
}
#自定义的变量
fastcgi_param PATH_INFO $path_info;
#脚本请求文件路径 html/index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
#脚本名index.php
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
}
当然,php如果没开启也需要开启,修改php.ini将cgi.fix_pathinfo=1 的注释去掉,如果没注释=0的话也不行,需要改成1支持。