Home  >  Article  >  PHP Framework  >  About ThinkPHP5 configuring Nginx to achieve compatible pathinfo mode access

About ThinkPHP5 configuring Nginx to achieve compatible pathinfo mode access

藏色散人
藏色散人forward
2020-09-14 10:07:512183browse

下面由thinkphp教程栏目给大家介绍ThinkPHP5配置Nginx实现兼容pathinfo模式访问,希望对需要的朋友有所帮助!

About ThinkPHP5 configuring Nginx to achieve compatible pathinfo mode access

ThinkPHP项目,当使用Apache服务器时,Apache服务器本身支持pathinfo模式,ThinkPHP的4种访问模式都没问题;当使用Nginx时,碰到问题,必须使用兼容模式访问。
查找很多文档,归纳原因及解决方案如下:

原因:低版本的Nginx不支持pathinfo模式;高版本的Nginx(大约0.7以后)支持,但默认没有开启。
解决解决方案:

1、开启Nginx的pathinfo模式:
    在站点配置文件中找到 location ~ \.php$ {...} 那部分,把那个 $ 符号删除,然后在{}中添加以下代码:

    fastcgi_split_path_info         ^(.+\.php)(.*)$;
    fastcgi_param  PATH_INFO        $fastcgi_path_info;    
    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;

    保存,重启。这时Nginx已经支持pathinfo,访问www.xxx.com/index.php/admin/index/test查看结果
    注意:如果模板文件中使用了U()函数,fastcgi_split_path_info部分要替换为

    fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;

2、rewrite重写,隐藏入口文件index.php
    在站点配置文件中找到 location / {...}这部分,在里面添加以下代码:

    # 专门针对tp的rewrite模式
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=$1  last;
        break;
    }

    保存,重启。这时Nginx完美支持pathinfo模式,访问www.xxx.com/admin/index/test查看结果

The above is the detailed content of About ThinkPHP5 configuring Nginx to achieve compatible pathinfo mode access. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete