Nginx 同一端口下部署多个 Vue3 项目

前言

前端多项目部署到 Nginx 的同一监听端口下的解决方案,项目由一个主项目和多个子项目组成,主项目和子项目都是单独打包。

主子项目之间是使用的腾讯开源的无界(WebComponent 容器 + iframe 沙箱)前端框架,能够完善的解决适配成本、样式隔离、运行性能、页面白屏、子应用通信、子应用保活、多应用激活、vite 框架支持、应用共享等。

https://wujie-micro.github.io/doc

项目打包设置

vite.config.js文件中设置 base 路径:

主项目 base 路径设置为默认即可'/':

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base:'/',
  };
});

子项 base 路径设置为'/sub/'

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base:'/sub/',
  };
});

Nginx.conf 配置

server {
    listen       80;
    server_name  demo.com;

    # 主项目
    location / {
        root   /usr/web/main/;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    # 子项目
    location /sub {
        alias /usr/web/sub/;
        try_files $uri $uri/ /sub/index.html last;
        index index.html;
    }
}

注意

1、子项 base 设置的路径和 Nginx.conf 配置的子项目监听路径不一致页面刷新会报如下错:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

2、主项目 location 的是 root,而子项目中的是 alias

3、子项目 try_files index.html 需要配置前缀路径 /sub

访问

主项目:http://demo.com

子项目:http://demo.com/sub

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值