nginx配置ThinkPHP5二级目录访问

转自https://www.cnblogs.com/mracale/p/9468980.html

可以通过 http://www.mracale.com/项目名/模块名/方法名 进行访问

第一步

首先,你要确保在不配置二级目录的情况下,可以通过浏览器访问到。例如:http://www.mracale.com/blog/index.php?s=index/index/index

如果不能正常访问,报404错误,建议看一看你的nginx配置中是如何处理php的。因为ThinkPHP中index.php并不一定都是在URL中末尾出现的,所以要使用

location ~ .php($|/)

  而不是

location ~ .php$

例如如下配置:

location ~ \.php($|/) {
    root           /home/html;
    fastcgi_pass   unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

第二步

进行URL重写,将默认访问URL中的index.php?s=通过rewrite隐藏

location /blog/ {
    index index.php index.html index.htm;
    if (!-e $request_filename){
        rewrite ^/blog/(.*)$ /blog/index.php?s=$1 last;
    }
}

这样就可以通过 http://www.mracale.com/blog/index/index/index 来进行访问了。

其实nginx的二级目录配置都是一样的套路,这里也可以参考以前写过的另一篇配置记录:nginx配置phalcon

有的小伙伴配置后出现访问资源文件报错模块不存在错误,这里只需添加对静态资源文件的特殊处理即可,例如:

扫描二维码关注公众号,回复: 7383011 查看本文章
location ~ .*\.(css|js|gif|jpg|jpeg|png|bmp|swf)$ {
     root         /home/html;
     expires      30d;
 }

猜你喜欢

转载自www.cnblogs.com/baker95935/p/11602002.html