关于Nginx不支持pathinfo问题解决

在nginx下是不支持pathinfo的,但是apache支持pathinfo。
pathinfo是什么?
首先我们在nginx的html目录下新键1.php文件,打印$_SERVER

<?php
echo "<pre>";
print_r($_SERVER);

尝试在地址栏输入如下参数
在这里插入图片描述
结果返回404
在这里插入图片描述
同样的代码在apache下测试
在这里插入图片描述
没有报错 并且页面多了PATH_INFO的字段 值为 url地址后面的参数 a/b/c 这就是pathinfo
一些框架中他的地址栏格式为 index.php/Home/Index/goods/1…这种格式在apache中可以正常解析,但在nginx中是不支持的,也就是你的框架项目直接拿到nginx上是跑不起的。
解决办法如下

//基础配置
location ~ \.php$ { 
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /$DOCUMENT_ROOT$fastcgi_script_name;
           include        fastcgi_params;
}
//修改后的配置
location ~ \.php(.*)$ {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /$DOCUMENT_ROOT$fastcgi_script_name;
           fastcgi_param  PATH_INFO $1;
           include        fastcgi_params;
       }

通过正则反向引用将.php后面的参数传递给pathinfo
浏览器测试传入参数 1.php/a/b
页面生成PATH_INFO字段
在这里插入图片描述
此时解决Nginx不支持pathinfo的问题。

但有些框架他的地址栏格式是这样的
域名+a/b/c,域名后面没有index.php
dian.com/show/eic
这时候要使你的项目在nginx上运行就要通过url地址重写解决问题

 location / {
      root html/daikuan/public;
      index index.php index.html ;
      //添加url重写
      if ( !-e $request_filename) {
           rewrite (.*)$ /index.php/$1;
      }

但是这种方式一直导致
500 Internal Server Error
不知道什么原因,不过在nginx中还可以通过try_files解决上述问题

location / {
            root html/daikuan/public;
            index index.php index.html ;
            //tryfile
           try_files $uri /index.php?$uri;
        }

发布了91 篇原创文章 · 获赞 3 · 访问量 3190

猜你喜欢

转载自blog.csdn.net/weixin_45143481/article/details/104900299
今日推荐