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

相关推荐
初学者_xuan1 小时前
Linux程序与进程和进程程序基础以及程序管理(零基础掌握版)
运维·计算机网络·网络安全·零基础·学习方法·linux程序管理
小白银子1 小时前
零基础从头教学Linux(Day 20)
linux·运维·服务器·php·国安工程师
再学一点就睡1 小时前
深入理解 Redux:从手写核心到现代实践(附 RTK 衔接)
前端·redux
天上掉下来个程小白1 小时前
微服务-27.配置管理-什么是配置管理
运维·微服务·架构
古月-一个C++方向的小白2 小时前
Linux初始——基础指令篇
linux·运维·服务器
天天进步20152 小时前
从零到一:现代化充电桩App的React前端参考
前端·react.js·前端框架
古月-一个C++方向的小白2 小时前
初始Linux——指令与权限
linux·运维·服务器
柯南二号2 小时前
【大前端】React Native Flex 布局详解
前端·react native·react.js
龙在天3 小时前
npm run dev 做了什么❓小白也能看懂
前端
hellokai4 小时前
React Native新架构源码分析
android·前端·react native