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

前言

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

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

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

项目打包设置

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

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

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

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

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

Nginx.conf 配置

shell 复制代码
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

相关推荐
Joy T1 小时前
【AI运维】02 云上基础部署:ECS、OSS 与 Nginx 的体系化理解与实践
运维·nginx
Dreamboat-L2 小时前
云服务器上部署nginx
java·服务器·nginx
石小千3 小时前
Nexus升级(3.63.0--3.87.1)
运维
魂万劫4 小时前
如何在虚拟机VM上|Linux环境内安装windows
linux·运维·服务器·windows
季__末4 小时前
WSL2安装配置
nginx
数字化转型20255 小时前
SAP Signavio 在风机制造行业的深度应用研究
大数据·运维·人工智能
WordPress学习笔记5 小时前
wordpress根据分类ID调用分类名称和分类描述
运维·wordpress
qq_455760856 小时前
docker - 镜像、存储卷和网络深入理解
运维·docker·容器
九思x7 小时前
Linux 系统安装 JDK 17
linux·运维
HIT_Weston8 小时前
77、【Ubuntu】【Hugo】搭建私人博客:Detached HEAD
linux·运维·ubuntu