一个域名多个vue项目

文章介绍了如何在同一个域名下设置不同路径来部署多个Vue项目,涉及到Vue的base和publicPath配置,以及Nginx的try_files和alias指令。同时,讨论了如何通过另一个域名B代理到域名A的特定项目,包括直接代理、指定项目代理和全量代理的不同方式及其优缺点。
摘要由CSDN通过智能技术生成

同域名不同项目

在同一个域名下根据不同路径设置不同项目,主要是配置nginx,并与vue项目的路由模式有关。

一、一个域名多个项目

项目一:https://域名/apple
项目二:https://域名/banana

1.首先设置vue项目打包配置
  1. 路由中配置base,例如“base: /apple/”
  2. 在vue.config.js文件中配置publicPath,例如 ”publicPath: /apple/“
2.上传文件到服务器

在这里插入图片描述

3.配置nginx

重点是try_files,刷新出现404就是try_files配置有问题。

    location /apple {
      # root /www/wwwroot/域名/apple;
      alias /www/wwwroot/域名/apple;
      try_files $uri $uri/ /apple/index.html;
      index index.html;
    }
    location /banana {
      # root /www/wwwroot/域名/apple;
      alias /www/wwwroot/域名/banana;
      try_files $uri $uri/ /banana/index.html;
      index index.html;
    }
4.效果

在这里插入图片描述

在这里插入图片描述

二、域名指向其他域名下的项目

例如有两个域名A、B,在A域名下有historyhashapplebanana四个项目,通过B域名访问A域名下的项目。
history、hash 项目未配置 base 和 publicPath。
apple、banana 项目已配置 base 和 publicPath。

四种方法:

1.B域名直接加项目后面的的地址

history项目在A中地址:https://A/history | https://A/history/style
使用B域名访问的地址为:https://B | https://B/style

这个不需要配置路由文件的base和配置文件中的publicPath

优点:B直接访问history项目,不需要在B域名后面加项目名
缺点:A/history无法访问查看页面效果。

A的nginx配置:

    location /history {
      alias /www/wwwroot/A/history;
      try_files $uri $uri/ /history/index.html;
      index index.html;
      # root /www/wwwroot/A;
      # try_files $uri $uri/ /history/index.html;
      # index /history/index.html;
    }
    location /hash {
      alias /www/wwwroot/A/hash;
      try_files $uri $uri/ /hash/index.html;
      index index.html;
    }

B的nginx配置:

    location ^~ / {
      proxy_pass https://A/history/;
    }

2.B域名只能代理A域名指定项目

apple 项目在A中地址:https://A/apple | https://A/apple/style
使用B域名访问的地址为:https://B/apple | https://B/apple/style

这个 需要 配置路由文件的base和配置文件中的publicPath

优点:可以控制哪些项目可以访问,可以使用A,B两个域名访问
缺点:需要配置`base`和`publicPath`

A的nginx配置:

    location /apple{
      alias /www/wwwroot/A/apple;
      try_files $uri $uri/ /apple/index.html;
      index index.html;
      # root /www/wwwroot/A;
      # try_files $uri $uri/ /apple/index.html;
      # index /apple/index.html;
    }
    location /banana {
      alias /www/wwwroot/A/banana ;
      try_files $uri $uri/ /banana/index.html;
      index index.html;
    }

B的nginx配置:

    # 只代理apple项目
    location ^~ /apple {
      proxy_pass https://A/apple/;
    }

3.B域名只能代理A域名指定项目

apple 项目在A中地址:https://A/apple | https://A/apple/style
使用B域名访问的地址为:https://B(https://B/apple) | https://B/style(https://B/apple/style)

这个 需要 配置路由文件的base和配置文件中的publicPath

优点:可以控制哪些项目可以访问,可以使用A,B两个域名访问
缺点:需要配置`base`和`publicPath`

A的nginx配置:

    location /apple{
      alias /www/wwwroot/A/apple;
      try_files $uri $uri/ /apple/index.html;
      index index.html;
      # root /www/wwwroot/A;
      # try_files $uri $uri/ /apple/index.html;
      # index /apple/index.html;
    }
    location /banana {
      alias /www/wwwroot/A/banana ;
      try_files $uri $uri/ /banana/index.html;
      index index.html;
    }

B的nginx配置:

    location ^~ / {
      rewrite ^/(.*)$ /apple/$1 break;
      proxy_pass https://car.lichee.top;
    }
    location ^~ /apple {
      proxy_pass https://car.lichee.top;
    }

4.B域名代理全部A域名的项目

项目在A域名中的地址:https://A/apple | https://A/banana
使用B域名访问的地址:https://B/apple | https://B/banana

这个 需要 配置路由文件的base和配置文件中的publicPath

优点:在A,B两个域名都可以访问,相当于复制了A域名下所有项目
缺点:需要配置`base`和`publicPath`
    location /apple {
      # root /www/wwwroot/A/apple;
      alias /www/wwwroot/A/apple;
      try_files $uri $uri/ /apple/index.html;
      index index.html;
    }
    location /banana {
      # root /www/wwwroot/A/banana;
      alias /www/wwwroot/A/banana;
      try_files $uri $uri/ /banana/index.html;
      index index.html;
    }

B的nginx配置:

    location ^~ /{
      proxy_pass https://A/;
    }

综上,一个域名多个项目或者由其他项目进行代理转发主要就是与nginx的配置有关。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sea_lichee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值