• nginx中配置pathinfo模式示例


    要想让nginx支持PATH_INFO,首先需要知道什么是pathinfo,为什么要用pathinfo?

    pathinfo不是nginx的功能,pathinfo是php的功能。

    php中有两个pathinfo,一个是环境变量$_SERVER['PATH_INFO'];另一个是pathinfo函数,pathinfo() 函数以数组的形式返回文件路径的信息;。

    nginx能做的只是对$_SERVER['PATH_INFO]值的设置。

    下面我们举例说明比较直观。先说php中两种pathinfo的作用,再说如何让nginx支持pathinfo。

    php中的两个pathinfo

    php中的pathinfo()

      pathinfo()函数可以对输入的路径进行判断,以数组的形式返回文件路径的信息,数组包含以下元素。

    • [dirname]  路径的目录
    • [basename] 带后缀 文件名
    • [extension]  文件后缀
    • [filename]  不带后缀文件名(需php5.2以上版本)

    例如

    复制代码
    print_r(pathinfo("/nginx/test.txt"));
    
    输出:
    
    Array
    (
        [dirname] => /nginx
        [basename] => test.txt
        [extension] => txt
        [filename] => test
    )
    复制代码

    php中的$_SERVER['PATH_INFO']

      PHP中的全局变量$_SERVER['PATH_INFO'],PATH_INFO是一个CGI 1.1的标准,经常用来做为传参载体。

      被很多系统用来优化url路径格式,最著名的如THINKPHP框架。

      对于下面这个网址:

      http://www.test.cn/index.php/test/my.html?c=index&m=search

      我们可以得到 $_SERVER['PATH_INFO'] = ‘/test/my.html’,而此时 $_SERVER['QUERY_STRING'] = 'c=index&m=search';

      如果不借助高级方法,php中http://www.test.com/index.php?type=search 这样的URL很常见,大多数人可能会觉得不太美观而且对于搜索引擎也是非常不友好的(实际上有没有影响未知),因为现在的搜索引擎已经很智能了,可以收入带参数的后缀网页,不过大家出于整洁的考虑还是想希望能够重写URL,

      下面是一段解析利用PATH_INFO的进行重写的非常简单的代码:

    复制代码
    if(!isset($_SERVER['PATH_INFO']))
    {
        $pathinfo = 'default';
    }
    else{
        $pathinfo = explode('/', $_SERVER['PATH_INFO']);
    }
    
    if(is_array($pathinfo) && !empty($pathinfo))
    {
        $page = $pathinfo[1];
    }
    else
    {
        $page = 'default.php';
    }
    复制代码

      有了以上认识我们就可以介入nginx对$_SERVER['PATH_INFO']支持的问题了。在这之前还要介绍一个php.ini中的配置参数cgi.fix_pathinfo,它是用来对设置cgi模式下为php是否提供绝对路径信息或PATH_INFO信息。没有这个参数之前PHP设置绝对路径PATH_TRANSLATED的值为SCRIPT_FILENAME,没有PATH_INFO值。设置这个参数为cgi.fix_pathinfo=1后,cgi设置完整的路径信息PATH_TRANSLATED的值为SCRIPT_FILENAME,并且设置PATH_INFO信息;如果设为cgi.fix_pathinfo=0则只设置绝对路径PATH_TRANSLATED的值为SCRIPT_FILENAME。cgi.fix_pathinfo的默认值是1。

      nginx默认是不会设置PATH_INFO环境变量的的值,需要php使用cgi.fix_pathinfo=1来完成路径信息的获取,但同时会带来安全隐患,需要把cgi.fix_pathinfo=0设置为0,这样php就获取不到PATH_INFO信息,那些依赖PATH_INFO进行URL美化的程序就失效了。

    1.可以通过rewrite方式代替php中的PATH_INFO

      实例:thinkphp的pathinfo解决方案
      设置URL_MODEL=2

    location / {
        if (!-e $request_filename){
            rewrite ^/(.*)$ /index.php?s=/$1 last;
        }
    }

    2.nginx配置文件中设置PATH_INFO值
      请求的网址是/abc/index.php/abc

      PATH_INFO的值是/abc
      SCRIPT_FILENAME的值是$doucment_root/abc/index.php
      SCRIPT_NAME /abc/index.php

      旧版本的nginx使用如下方式配置

    复制代码
    location ~ .php
            {
                    #fastcgi_pass  unix:/tmp/php-cgi.sock;
                    fastcgi_pass  127.0.0.1:9000;
                    fastcgi_index index.php;
                    include        fastcgi_params;
                    set $real_script_name $fastcgi_script_name;
                    if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
                            set $real_script_name $1;
                            set $path_info $2;
                    }
                    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                    fastcgi_param SCRIPT_NAME $real_script_name;
                    fastcgi_param PATH_INFO $path_info;
            }
    复制代码

    整理之后 nginx.conf 的代码:

    复制代码
         location / {
                    if (!-e $request_filename) {
                            rewrite  ^/(.*)$  /index.php/$1  last;
                            break;
                    }
            }
    
            location ~ .php
            {
                    #fastcgi_pass  unix:/tmp/php-cgi.sock;
                    fastcgi_pass  127.0.0.1:9000;
                    fastcgi_index index.php;
                    include        fastcgi_params;
                    set $real_script_name $fastcgi_script_name;
                    if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
                            set $real_script_name $1;
                            set $path_info $2;
                    }
                    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                    fastcgi_param SCRIPT_NAME $real_script_name;
                    fastcgi_param PATH_INFO $path_info;
            }
    复制代码

     最后总结: 【注意:location ~ .php {   是没有 $ 的 ,原来是  location ~ .php$ {,所以记住这个也要改到】

      只要修改nginx里面配置文件就行:

    location / {
      root /usr/share/nginx/html/tp5/public;
      index index.php index.htm;
      if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php/$1 last;
        break;
      }
    }

    location ~ .php {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      set $real_script_name $fastcgi_script_name;
      if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
        set $real_script_name $1;
        set $path_info $2;
      }
      fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
      # fastcgi_param SCRIPT_NAME $real_script_name;
      fastcgi_param PATH_INFO $path_info;
    }

  • 相关阅读:
    第10节--单行函数之数学函数
    第九节--单行函数之日期函数
    【译】什么是游戏开发实体系统框架(终)
    【译】什么是游戏开发实体系统框架(一)
    【译】游戏《实体系统框架》译文——序
    IDEA 快捷键
    Java学习笔记1
    Java面试题
    Java学习笔记——集合
    Java学习笔记——String类常用方法
  • 原文地址:https://www.cnblogs.com/hfdp/p/5867844.html
Copyright © 2020-2023  润新知