〇. 前言

pathinfo有两个,1 pathinfo()函数,2 $_SERVER['PATH_INFO']。
pathinfo()是php的库函数,原生支持不需要nginx配置,$_SERVER['PATH_INFO']才需要nginx的特殊配置。
为什么要配置pathinfo,因为ThinkPHP/CodeIgniter/Yii等MVC框架都需要$_SERVER['PATH_INFO']。

一. 配置pathinfo支持之前,先让nginx支持PHP

这里有个缺陷,就是得到的SCRIPT_NAME不准确。

二. 开始配置 pathinfo

这是传统的nginx - pathinfo 解决方案

三. 寻找最佳方案的途中

因为配置文件里面PATH_INFO 跟 SCRIPT_NAME 都是使用了 $fastcgi_script_name,所以strace追踪nginx进程发现两者一样是正常的。可是从浏览器请求的效果看到PATH_INFO跟SCRIPT_NAME却不一样,虽然从最终结果看,它符合我们的要求,可是不合理呀。
我猜想,是php-fpm或者说fastcgi对此做了一些容错处理。

四. 最佳方案

五. rewrite 隐藏index.php

另外,现在nginx官方推荐使用try_files取代rewrite,不过try_files好像不支持正则,有兴趣的童鞋自行研究哈。给出一个例子,如下

六. 附件

1) 上面用到的配置文件的合集

 server {
listen ;
server_name test;
charset utf-; index index.html index.htm index.php;
root /var/www/test; # rewrite 隐藏index.php
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$ last;
break;
}
} # 传统方案
#location ~ \.php {
# set $real_script_name $path_info;
#
# if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
# set $real_script_name $;
# set $path_info $;
# }
#
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
# fastcgi_param PATH_INFO $path_info;
# fastcgi_param SCRIPT_NAME $real_script_name;
#
# fastcgi_pass 127.0.0.1:;
#} # 寻找最佳方案的途中
#location ~ \.php {
# include fastcgi.conf;
# fastcgi_param PATH_INFO $fastcgi_script_name;
#
# fastcgi_pass 127.0.0.1:;
#} # 最佳方案
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi.conf;
fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass 127.0.0.1:;
} }

2) fastcgi.conf 的中文注释

 fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name; #脚本文件请求的路径
fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=
fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。 fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
fastcgi_param REQUEST_URI $request_uri; #请求的地址
fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/.0或HTTP/1.1。 fastcgi_param GATEWAY_INTERFACE CGI/1.1; #cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; #nginx 版本号,可修改、隐藏 fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
fastcgi_param REMOTE_PORT $remote_port; #客户端端口
fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
fastcgi_param SERVER_PORT $server_port; #服务器端口
fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name # PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS ; 在php可打印出上面的服务环境变量
如:echo $_SERVER['REMOTE_ADDR']

3) nginx 的uri、request_uri 区别

$request_uri: /stat.php/qw/cv?id=1585378&web_id=1585378
$uri /stat.php/qw/cv
$document_uri: /stat.php/qw/cv

3) 检验 REQUEST_URI / DOCUMENT_URI / PHP_SELF / PATH_INFO

echo 'REQUEST_URI - '.$_SERVER['REQUEST_URI'];
echo '<br />DOCUMENT_URI - '.$_SERVER['DOCUMENT_URI'];
echo '<br />SCRIPT_NAME - '.$_SERVER['SCRIPT_NAME'];
echo '<br />PHP_SELF - '.$_SERVER['PHP_SELF'];
echo '<br />PATH_INFO - '.$_SERVER['PATH_INFO'];

同时strace 追踪nginx进程

nginx配置pathinfo支持,最佳方案 - chunyu的更多相关文章

  1. [记录]NGINX配置HTTPS性能优化方案一则

    NGINX配置HTTPS性能优化方案一则: 1)HSTS的合理使用 2)会话恢复的合理使用 3)Ocsp stapling的合理使用 4)TLS协议的合理配置 5)False Start的合理使用 6 ...

  2. windows 下nginx配置php支持

    修改nginx配置 location ~ \.php$ { root D:/Learn/php/test/; fastcgi_pass ; fastcgi_index index.php; fastc ...

  3. Centos7.2下Nginx配置SSL支持https访问(站点是基于.Net Core2.0开发的WebApi)

    准备工作 1.基于nginx部署好的站点(本文站点是基于.Net Core2.0开发的WebApi,有兴趣的同学可以跳http://www.cnblogs.com/GreedyL/p/7422796. ...

  4. nginx 配置虚拟机 支持pathinfo

    server { server_name shopx.local *.shopx.local; charset utf-8; root /Users/x/www/php/shopx.local/sho ...

  5. Nginx 开启PATHINFO支持ThinkPHP框架实例

    ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可.在Apache下只需要开启mod_rew ...

  6. Nginx配置PATHINFO隐藏index.php

    1.网络来源:http://www.shouce.ren/post/view/id/1529 server {      listen       80;     default_type text/ ...

  7. [转]Nginx+ThinkPHP不支持PathInfo的解决办法

    FROM : http://www.4wei.cn/archives/1001174 应集团要求,公司的服务器全收到集团机房统一管理了,失去了服务器的管理配置权限. 杯具就此开始. 首先要解决文件大小 ...

  8. MNMP下nginx1.6开启支持pathinfo配置,支持thinkphp的URL格式

    对于thinkphp框架项目的访问格式有多种,其中pathinfo是默认的基本访问格式,格式为:http://hostname:port/index.php/模块名/action名/参数1/参数1的值 ...

  9. Mac直播服务器Nginx配置对HLS的支持

    在上一篇中Mac上搭建直播服务器Nginx+rtmp,我们已经搭建了nginx+rtmp直播服务器.下面需要对Nginx服务器增加对HLS的支持.在Nginx增加对HLS种支持比较简单,只是简单的修改 ...

随机推荐

  1. IIS发布报错

    IIS发布报错一般原因 ISAPI和CGI限制作为IIS与ASP.NET的连接桥梁

  2. python写的第一个简单小游戏-猜数字

    #Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...

  3. sort+结构体实现二级排序

    之前介绍的sort函数由于其效率较高,使用较为简单让我用起来那叫一个爽,今天再写一篇使用sort+结构体实现二级排序的方法. 还是先想个问题吧,比如我想输入5个同学的名字和身高,然后得到他们身高的降序 ...

  4. How can I add a new user as sudoer using the command line?

    Two ways to use sudo command for a standard user account: First, If you want to use sudo command for ...

  5. 自定义progressbar

    <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content&quo ...

  6. android开发调用c++共享库so文件

    1.编写libaab.cpp #include <stdio.h>#include <stdlib.h> #ifdef __cplusplusextern "C&qu ...

  7. (转)在mac上配置cocos2d-x开发环境

    转自:http://www.cnblogs.com/xiaodao/archive/2013/01/08/2850751.html 一.下载cocos2d-x最新安装包 在终端中cd到本地将要存放目录 ...

  8. Java [Leetcode 125]Valid Palindrome

    题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  9. Java [Leetcode 223]Rectangle Area

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  10. atoi&itoa

    char* itoa(int num,char*str,int radix) {/*索引表*/ char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVW ...