前端多个项目部署在同一个nginx下,前缀不同,配置编写方式

我们前端是微前端的项目,不同模块是分开的不同项目,用访问前缀区分。开发环境部署为了节约资源,直接使用一个nginx当做静态资源服务器,服务多个微前端,示意图如下:

下面是nginx使用的配置(server部分)

conf 复制代码
server {
        listen       8888;
        server_name  localhost;
        access_log  /var/log/nginx/mobile-access.log ;


        # 静态资源
        location /project1 {
          alias /deploy/project1;
          index index.html;
          try_files $uri $uri/ /project1 /index.html;
          autoindex off;
        }

        location /project2{
          alias /deploy/project2;
          index index.html;
          try_files $uri $uri/ /project2/index.html;
          autoindex off;
        }
        
        location /project3{
          alias /deploy/project3;
          index index.html;
          try_files $uri $uri/ /project3/index.html;
          autoindex off;
        }

        location /api {
          proxy_pass http://127.0.0.1:8080;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_connect_timeout 300;
          proxy_read_timeout 300;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /www/html;
        }
}

特别注意的是,由于是前端项目, try_files 部分要按照上面的写,否则编译以后不能自动获取index.html,也就无法加载项目。

文件中/api部分为示意,一般是把流量转给后端网段。

相关推荐
xiangxiongfly91526 分钟前
CSS link标签
前端·css
AI智域边界 - Alvin Cho1 小时前
Bloomberg、LSEG 与 MCP 缺口:为什么尚未发布完整的 MCP 服务器,以及多智能体系统如何解決这问题
运维·服务器
岁月宁静1 小时前
AI 多模态全栈应用项目描述
前端·vue.js·node.js
_OP_CHEN2 小时前
Linux网络编程:(七)Vim 编辑器完全指南:从入门到精通的全方位实战教程
linux·运维·服务器·编辑器·vim·linux生态·linux软件
Maple_land2 小时前
第1篇:Linux工具复盘上篇:yum与vim
linux·运维·服务器·c++·centos
nn_(nana)2 小时前
修改文件权限--- chmod ,vi/vim,查看文件内容,yum-软件包管理器,systemctl管理系统服务
前端
shizhan_cloud2 小时前
Linux 硬盘分区管理
linux·运维
烛阴3 小时前
从零开始掌握C#核心:变量与数据类型
前端·c#
蓁蓁啊3 小时前
Ubuntu 虚拟机文件传输到 Windows的一种好玩的办法
linux·运维·windows·单片机·ubuntu