nginx配置ThinkPHP5二级目录访问

第一步

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

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

1
location ~ .php($|/)

而不是

1
location ~ .php$

例如如下配置:

1
2
3
4
5
6
7
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隐藏

1
2
3
4
5
6
location /blog/ {
    index index.php index.html index.htm;
    if (!-e $request_filename){
        rewrite ^/blog/(.*)$ /blog/index.php?s=$1 last;
    }
}<br>

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

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

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

1
2
3
4
location ~ .*\.(css|js|gif|jpg|jpeg|png|bmp|swf)$ {
     root         /home/html;
     expires      30d;
 }

nginx 静态文件二级目录

 location /xxx/ {      #二级目录设置
      root  /www/wwwroot/default;   #不需要写xxx,root会补齐
      index login.html;   #默认的访问页面
      autoindex on;#自动扫描
      expires 30s;#缓存设置
      location ~ ^/xxx/.*\.(gif|jpg|jpeg|png|bmp|swf|flv)?$ {
                root /www/wwwroot/default; #不需要写blood-manage,root会补齐
                expires 30d;
        }#设置静态文件目录
        location ~ ^/xxx/.*\.(js|css)?$ {
                root /www/wwwroot/default; #不需要写blood-manage,root会补齐
                expires 1h;

        }#设置静态文件目录
    }


二级目录反向代理

  1. location /houtai/ {
  2. proxy_set_header Host $host;
  3. proxy_set_header X-Real-IP $remote_addr;
  4. proxy_set_header X-Forwarded-For $remote_addr;
  5. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  6. proxy_set_header Remote_addr $remote_addr;
  7. proxy_pass http://127.0.0.1:9888/;
  8. }

    但是会造成js css 静态文件无法访问

    vue 2.6.x

    vue.config.js

    publicPath: process.env.NODE_ENV === 'production' ? '/houtai' : '/',
    

    就可以了

    可以在dist文件夹,index.html文件 引用文件是否有 houtai 前缀