nginx下通过子路径配置多个vue单页应用的方法

千辛万苦在Stack Overflow上找来的,记下吧。

https://stackoverflow.com/q/31519505/13651734

我的需求是

首页:/

项目a:/aaa

项目b:/bbb

 

server {
    listen [::]:80 default_server;
    listen 80;
    server_name localhost mydomain 127.0.0.1;
    root C:/manager/houchejishi;

    location / {
        alias C:/manager/houchejishi/portal/dist/;
        index  index.html;
        try_files $uri $uri/ index.html =404;
    }

    location /h5 {
        alias C:/manager/houchejishi/h5/dist/;
        index  index.html;
        try_files $uri $uri/ index.html =404;
    }

    location /system {
        alias C:/manager/houchejishi/system/dist/;
        index  index.html;
        try_files $uri $uri/ index.html =404;
    }
}

 

另外,如果用宝塔建立的网站的话,把网站单独的nginx配置中的location ~ .*\.(js|css)?$和location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$两块代码注释掉,就可以正常家在js,css和图片资源文件了。

 

补充:

有时候某些环境下的nginx会出现访问不带/的uri时被重定向的情况,这时要在server下加上absolute_redirect off;这个设置即可解决。

例如:

server {
      listen 80;
      listen [::]:80;

      server_name example.com;
    absolute_redirect off;

    location / {...}
}

 

posted @ 2020-05-31 16:16  spikespiegel  阅读(524)  评论(0)    收藏  举报