开发者社区> 问答> 正文

Linux主机宝或纯环境开启PATHINFO的方法


Pathinfo路径路由模式是php5的一种url地址解释模式,而nginx默认是不支持pathinfo的路由模式的,本文介绍通过修改nginx配置文件使其支持php的pathinfo路径模式。
Nginx模式默认是不支持pathinfo模式的,类似info.php/hello形式的url会被提示找不到页面。例如CI框架,在不隐藏index.php,没有使用伪静态的情况下,就需要PATHINFO支持。
以下3步让指定的站点支持PATHINFO:


1、编辑需要开启PATHINFO站点的配置文件: /a/apps/nginx/vhosts/站点域名.conf
     在 access_log xxx main; 下面增加:
set $real_script_name $fastcgi_script_name;
        set $path_info "";
        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;


2、注释掉 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 变成:
#fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
3、修改 location ~ \.php$ {  为:
location ~ \.php(/.*)?$ {

改完后如图:



重启 Nginx 生效, service nginx restart


展开
收起
八方网络 2014-06-02 14:35:08 25495 0
13 条回答
写回答
取消 提交回答
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    麻烦高手给看看我的这个配置有什么问题吗?一直报502 Bad Gateway

    server
      {
        listen 80;
        server_name test.sup.cn;
        index index.html index.htm index.php;
        root /data/wwwroot/SuP;
        access_log /data/wwwlogs/access_nginx.log combined;

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

       location ~ \.php {
           fastcgi_pass 127.0.0.1:9000;
           fastcgi_index index.php;
           include fastcgi.conf;
           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;
       }
    }
    2016-04-21 14:50:36
    赞同 展开评论 打赏
  • 回13楼八方网络的帖子
    这个发放不行,最终结果是两个都不能访问。。。。请求解决办法
    2015-10-23 00:50:09
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    thinkphp 求大神!指点
    2015-05-03 05:37:08
    赞同 展开评论 打赏
  • 回 14楼(冒烟男) 的帖子
    是在找不到可以搜下
    2015-04-10 14:41:52
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法

    /a/apps/nginx/vhosts/站点域名.conf

    能详细点嘛?我是新手。那个文件在那改?我找不到!
    2015-04-09 20:23:43
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    我改了过后必须是/index.php/c/m才能访问,而/c/m不行,前面必须加个index.php


    配置文件是这样的,哪里有问题呢

    erver{
        listen 80;
        server_name 123.57.150.24;
        root /a/domains/123.57.150.24/public_html;
        index index.html index.htm index.shtml index.php;


        error_page  404               /404.html;
        #Custom rules Start
            #Custom rules End
        location = /500.html {
            root   /usr/share/nginx/html;
        }


        location ~ \.php(/.*)?$ {
            fastcgi_pass   unix:/dev/shm/php.sock;
            include        fastcgi_params;
            #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            access_log     /a/apps/nginx/logs/123.57.150.24.access.log main;
            set $real_script_name $fastcgi_script_name;
            set $path_info "";
            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 /
        {
            if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?$1 last;
            break;
            }
        }
        
    2015-03-27 03:56:39
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    我用wdcp,nginx+apache,打开站点域名.conf   没有   access_log  这样的代码,求大神
    2015-02-03 20:30:42
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    mark 一下
    2014-11-23 14:12:36
    赞同 展开评论 打赏
  • 回8楼acunion的帖子
    好贴。
    2014-09-18 23:36:29
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    搞了一晚上,才发现直接复制过去或有隐藏编码问题,终于搞定了,都是换行的一些其他的东西
    2014-08-03 01:37:17
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    我网站就是用CI框架的,联系了两次技术支持,还是有点小问题,现在看了这个教程自己调好了。另外还有两个问题,一是网址不支持大写,二是CI框架网址怎么去掉index.php,希望能出个教程解决一下。
    2014-06-23 14:22:34
    赞同 展开评论 打赏
  • 回1楼蜗居网的帖子
    您好,方法是一样的,修改您的站点配置文件。

    -------------------------

    您好CI框架去掉index.php ,如果URL不是以/? 开始,是通过伪静态实现的。
    详细请见(伪静态规则请参考本文最后):
    http://gongweicui.blog.163.com/blog/static/14211065520121296225374/

    如果使用了伪静态,就不需要开启PATHINFO支持了。

    CI Nginx伪静态规则:
    if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?$1 last;
            break;
        }


    -------------------------

    回 5楼(蜗居网) 的帖子
    您好,您的配置其中有错误:
    location ~ .*\.(php|php5)?$
    需要改为
    location ~ \.php(/.*)?$ {

    -------------------------

    set $real_script_name $fastcgi_script_name;
    set $path_info "";
    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 /
        {
            if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?$1 last;
            break;
            }
        }


    把  location / { }去掉 并把这段

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

    放到    location ~ \.php(/.*)?$ {  之前

    2014-06-18 16:40:50
    赞同 展开评论 打赏
  • ReLinux主机宝或纯环境开启PATHINFO的方法
    你好  请问下ECS nginx  环境下 怎么开启pathinfo呢

    -------------------------

    Re回2楼acunion的帖子
    请教下我的这个配置文件要怎么修改才行呢?我已经修改不下20次了还是没能弄好,还望你能赐教下指点一二。谢谢了。
    server {
            listen       80;
            server_name  localhost;
        index index.html index.htm index.php;
        root /alidata/www/phpwind;
        
        

        location ~ .*\.(php|php5)?$
        {
            #fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
            
            #path_info
            set $real_script_name $fastcgi_script_name;
            set $path_info "";
            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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires 1h;
        }
        #伪静态规则
        include /alidata/server/nginx/conf/rewrite/phpwind.conf;
        access_log  /alidata/log/nginx/access/phpwind.log;
    }
    2014-06-18 16:01:00
    赞同 展开评论 打赏
滑动查看更多
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载