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_index index.php;
- #先加载默认后解析赋值
- include fastcgi_params;
- #正则解析路径
- fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
- fastcgi_param PATH_INFO $fastcgi_path_info;
- fastcgi_param SCRIPT_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_index index.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
- (
- [USER] => www
- [HOME] => /home/www
- [FCGI_ROLE] => RESPONDER
- [QUERY_STRING] =>
- [REQUEST_METHOD] => GET
- [CONTENT_TYPE] =>
- [CONTENT_LENGTH] =>
- [SCRIPT_NAME] => /demos/doitnote/index.php //重点关注
- [REQUEST_URI] => /demos/doitnote/index.php/login/index
- [DOCUMENT_URI] => /demos/doitnote/index.php/login/index
- [DOCUMENT_ROOT] => /mnt/hgfs/vmhtml/doitphp
- [SERVER_PROTOCOL] => HTTP/1.1
- [GATEWAY_INTERFACE] => CGI/1.1
- [SERVER_SOFTWARE] => nginx/1.1.19
- [REMOTE_ADDR] => 192.168.127.1
- [REMOTE_PORT] => 60422
- [SERVER_ADDR] => 192.168.127.10
- [SERVER_PORT] => 80
- [SERVER_NAME] => [url]www.doitphp.loc[/url]
- [REDIRECT_STATUS] => 200
- [PATH_INFO] => /login/index //重点关注
- [SCRIPT_FILENAME] => /mnt/hgfs/vmhtml/doitphp/demos/doitnote/index.php //重点关注
- [HTTP_HOST] => [url]www.doitphp.loc[/url]
- [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
- [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- [HTTP_ACCEPT_LANGUAGE] => zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
- [HTTP_ACCEPT_ENCODING] => gzip, deflate
- [HTTP_CONNECTION] => keep-alive
- [HTTP_REFERER] => [url]http://www.doitphp.loc/demos/doitnote/[/url]
- [HTTP_CACHE_CONTROL] => max-age=0
- [PHP_SELF] => /demos/doitnote/index.php/login/index
- [REQUEST_TIME] => 1354246274
- )
复制代码 |