前端多个项目部署在同一个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部分为示意,一般是把流量转给后端网段。

相关推荐
这里有鱼汤15 分钟前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
用户214118326360244 分钟前
dify案例分享-免费玩转 AI 绘图!Dify 整合 Qwen-Image,文生图 图生图一步到位
前端
IT_陈寒1 小时前
Redis 性能翻倍的 7 个冷门技巧,第 5 个大多数人都不知道!
前端·人工智能·后端
mCell8 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip8 小时前
Node.js 子进程:child_process
前端·javascript
excel11 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel12 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼13 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping13 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙14 小时前
[译] Composition in CSS
前端·css