Nginx +ThinkPHP 遇到的问题

2020年7月14日 0 条评论 1.04k 次阅读 0 人点赞

Rewrite 规则
官方文档里的写法如下,参考 https://www.kancloud.cn/manual/thinkphp/1866

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

遇到问题,在一个项目中,所有的 __CONTROLLER__都带有 index.php。如果只使用上面的跳转,会造成 404 错误。所以又使用了:

server{
    #others xxx
    location / {
        if (!-e $request_filename) {
            rewrite \/index\.php(.*)$ $1 redirect;
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
    }
    #others xxxx
}

又遇到一个新的问题,如果是POST请求,数据就会丢失。所以还要用 proxy_pass。所以就变成了

server{
    #others xxx

    location ~ \/index\.php/(.*){
        proxy_pass http://yourdomain/$1;
        break;
    }
    location / {
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
    }
    #others xxxx

}

但又遇到了新的问题,访问的时候,会变成 502,查看 error.log,具体“no resolver defined to resolve ”,所以在 http 里,又需要加上 resolver 8.8.8.8;

http {
    resolver 8.8.8.8;
    # Others
}

至此:问题已经算是解决了。

补充,上面的配置又遇到个问题,发现有 index.php时,get参数会丢失。所以给proxy_pass又做了调整。增加了 ?args

server{
    #others xxx

    location ~ \/index\.php/(.*){
        proxy_pass http://yourdomain/$1?args;
        break;
    }
    location / {
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
    }
    #others xxxx

}

guojia

这个人太懒什么东西都没留下