ThinkPHP 无法加载控制器:Admin

更新日期: 2020-03-11 阅读次数: 6642 字数: 251 分类: thinkphp

测试环境

Nginx + ThinkPHP 3.2

现象

访问网站前端页面时,一切显示正常。

但是,访问网站管理后台,即以 /admin 为前缀路径的页面时,部分页面报错。

显示报错的页面路径

目前发现的有两类路径错误:

  • http://localhost/Admin/Index/Admin/page1.html 正确的路径应该是 http://localhost/Admin/page1.html
  • http://localhost/Admin/Index/Admin/Admin/page2.html 正确的路径应该是 http://localhost/Admin/page2.html

报错信息

无法加载控制器:Admin
错误位置
FILE: /some/path/ThinkPHP/Library/Think/App.class.php  LINE: 101

解决方法

在 nginx 配置中增加两条 rewrite 规则:

rewrite ^/Admin/Index/Admin/Admin/(.*)$ /index.php?s=/Admin/$1 last;
rewrite ^/Admin/Index/Admin/(.*)$ /index.php?s=/Admin/$1 last;

修改后的 nginx 配置为:

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

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}

nginx rewrite last / break 的区别

  • last:stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
  • break:stops processing the current set of ngx_http_rewrite_module directives as with the break directive;

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式