卡卡北 发表于 2018-12-27 21:05

Nginx下PHP支持path_info的方法(PHP/fastcgi)

Nginx(PHP/fastcgi)的PATHINFO配置

PATHINFO是一个CGI 1.1的标准,经常用来做为传参载体,在Apache中, 当不加配置的时候, 对于PHP脚本, AcceptPathInfo是默认接受的,而对于Nginx下, 是不支持PATHINFO 的, 也就是需要设置才能使用PATHINFO模式.

我们可以使用PATH_INFO来代替Rewrite来实现伪静态页面, 很多PHP框架也使用PATHINFO模式来作为路由载体

Apache 默认设置PATHINFO原理分析
#对于请求https://www.ys166.com/index.php/login/index

#Apache都接受, 都会认为是对index.php的访问, 并会设置PATH_INFO为'/login/index'

print_r($_SERVER["PATH_INFO"]);//login/index

nginx 模拟PATH_INFO

Nginx是通过对文件名的扩展名匹配,来决定是否要交给php cgi服务器去解释的.在nginx.conf中一般都有如下的默认配置段:
location ~ .php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
对于形如tools/index.php/login/index这样的文件路径, Nginx正则匹配为路径而不是php文件. 所以我们需要去掉($)改写这段配置匹配扩展名:
location ~ .php {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
现在, 脚本路径已经交由PHP自己处理了. 那怎么增加PATH_INFO呢?

方案一:php内置解析

我们需要打开PHP中cgi.fix_pathinfo配置项, 打开这个配置项以后, PHP会去根据CGI规范来检查SCRIPT_FILENAME中那部分是访问脚本和PATH_INFO(ini配置解释), 并根据SCRIPT_NAME来修改PATH_INFO(和PATH_TRANSLATED)为正确的值然后, 就只要添加一个FASTCGI_PARAM项就好了:
location ~ .php {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
}

方案二:nginx正则解析

上面的解决方法, 把对路径的分析交给了PHP去处理,下面是由Nginx来分析路径(也就不需要fix_pathinfo),两种配置方案
#配置方案 使用nginx模块fastcgi_split_path_info(nginx版本>0.7.31)
location ~ \.php {
fastcgi_pass   127.0.0.1:9000;
fastcgi_indexindex.php;
#先加载默认后解析赋值
include      fastcgi_params;
#正则解析路径
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_paramPATH_INFO      $fastcgi_path_info;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
}

/*
fastcgi_split_path_info
语法:fastcgi_split_path_info regex
使用字段:location
可用版本:0.7.31以上
这个指令允许为CGI specification设置SCRIPT_FILENAME、SCRIPT_NAME、PATH_INFO变量。正则包含两个组:
处理请求的脚本路径–>对应$fastcgi_script_name
脚本参数的值–>      对应$fastcgi_path_info
*/
location ~ ^.+\.php {
(...)
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO       $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
(...)
}
/*
示例请求”/show.php/article/0001”的参数SCRIPT_FILENAME将设置为”/path/to/php/show.php”,参数PATH_INFO为”/article/0001”*/

#配置方案2
#由于nginx内建只读变量$fastcgi_script_name无法赋值,所有通过设置$real_script_name变量来做中间值
location ~ \.php {
fastcgi_pass   127.0.0.1:9000;
fastcgi_indexindex.php;
#先加载默认后解析赋值
include      fastcgi_params;
#正则解析路径
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 PATH_INFO       $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME   $real_script_name;
}
/*nginx 把nginx解析url变量赋值给fastcgi_param,php通过$_SERVER获取fastcgi_param中的所有变量值 */
Array
(
=> www
=> /home/www
=> RESPONDER
=>
=> GET
=>
=>
=> /demos/doitnote/index.php//重点关注
=> /demos/doitnote/index.php/login/index
=> /demos/doitnote/index.php/login/index
=> /mnt/hgfs/vmhtml/doitphp
=> HTTP/1.1
=> CGI/1.1
=> nginx/1.1.19
=> 192.168.127.1
=> 60422
=> 192.168.127.10
=> 80
=> www.doitphp.loc
=> 200
=> /login/index//重点关注
=> /mnt/hgfs/vmhtml/doitphp/demos/doitnote/index.php//重点关注
=> www.doitphp.loc
=> Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
=> text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
=> zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
=> gzip, deflate
=> keep-alive
=> http://www.doitphp.loc/demos/doitnote/
=> max-age=0
=> /demos/doitnote/index.php/login/index
=> 1354246274
)

卡卡北 发表于 2018-12-27 21:06

其他方法:
nginx服务器开启phpinfo模式:
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
#try_files $uri =404; #把这行代码注释,让服务器不尝试去加载路径文件
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf; #加载phpinfo.conf配置
}

卡卡北 发表于 2018-12-27 21:27

相关:
什么是“path_info”
Apache下让PHP支持path_info的方法
Nginx下PHP支持path_info的方法
IIS下让PHP支持path_info的方法
页: [1]
查看完整版本: Nginx下PHP支持path_info的方法(PHP/fastcgi)