vue 多项目history模式在nginx下配置同一访问目录

vue的history模式设置参考:https://blog.csdn.net/youyudexiaowangzi/article/details/81777939

但是一个项目设置一个location的话,项目多了维护起来也麻烦,而且多是重复的,

于是想添加目录web,然后把所有项目放入web目录,所有location放入 location ^~ web {}中

配置如下:

nginx.conf的http模块中加入

    map $uri $match {
        default "notfound";
        ~^/web/hello1/ "hello1";
        ~^/web/hello2/ "hello2";
        #~^/web/([^\/]*)/ $1;
    }

这样如果访问

http://www.example.com/web/hello1/

http://www.example.com/web/hello1/sub2

都会匹配到变量$match, $match值为hello1,

然后再server模块加入

    location ^~ /web {
        root    /home/www/;

        try_files $uri $uri/ /web/$match/;
        #index   index.html index.htm;
    }

注意事项:

1.map多少还是会影响性能的

2.以前一般用if ($uri ~ ^/web/([!\/]*)) { set $match $1;}获取正则匹配的变量,但是用了if后,try_files就not found了,

http://www.example.com/web/hello1/可以访问,跳转到http://www.example.com/web/hello1/sub2也能正常使用,但是一刷新,http://www.example.com/web/hello1/sub2就not found了,

参考https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

所以要想获取正则返回的变量,就只想到map了。

发布了275 篇原创文章 · 获赞 46 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/youyudexiaowangzi/article/details/102599882