使用nginx部署多个vue项目

使用nginx部署多个vue项目

产生背景

最近公司要上线项目,由于囊中羞涩,需要在一台服务器的同一个域名下面部署多个项目,访问根路径(https://www.domain.com)是主项目,访问https://www.domain.com/project1 是项目1,访问https://www.domain.com/project2 是项目2,但是每个项目里面又有自己的路由,测试多次终于成功,遂记录以供后续查阅。

步骤一:不可忽略的打包配置

主项目、子项目配置

vue.config.js

router.js

步骤二:nginx配置

javascript 复制代码
// 主项目存放路径:/mydata/mainProject
// 项目1存放路径:/mydata/project1/
// 项目2存放路径:/mydata/project2/
server
{
    listen 80;
    server_name www.domain.com;


    location / {
        root /mydata/mainProject;
        index index.php index.html index.htm default.php default.htm default.html;
        try_files $uri $uri/ /index.html;
    }
    
    location ^~ /project1/ {
        alias /mydata/project1/;
        index index.php index.html index.htm default.php default.htm default.html;
        try_files $uri $uri/ /project1/index.html;
    }
    location ^~ /project2/ {
        alias /mydata/project2/;
        index index.php index.html index.htm default.php default.htm default.html;
        try_files $uri $uri/ /project2/index.html;
    }

    location /prod-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-Host $http_host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
}

结语

可能在部署过程中会遇到奇奇怪怪的问题,有可能是静态资源访问404,301等等,反正就是不断调整打包配置,重复修改nginx配置,最终目前方案可行(小声bb:全部当成子项目来部署方便得多,但是老板说主项目必须是访问域名就可以了,不能在根路径下加前缀)。

如果大家有更好的方案请多多指教。😜😜😜😜😜😜😜😜😜

相关推荐
AI源码6 小时前
高校查分系统
vue
北城笑笑2 天前
Vue 109 ,从零搭建 Vue3 + Electron 工程:生态、安全桥接与打包选型( Vue + Electron )
前端·javascript·vue.js·安全·electron·vue
上海懒图科技4 天前
工厂厂区电子地图定位导航:厂区人车精准定位,安全合规双保障
前端·arcgis·vue
卋罖7507 天前
Razor 视图 + JavaScript + Vue/iView 组件实现的表格自定义列
javascript·vue·iview·razor视图
天府云创10 天前
面向 Personal Agent 增强长期记忆与个性化能力,阿里云升级Memory 服务
阿里云·云计算·memory·agent智能体·长期记忆·personal agent
Elastic 中国社区官方博客10 天前
AgentHack Shanghai|基于阿里云AI搜索 Elasticsearch x Qoder 构建AI Agent应用
大数据·人工智能·elasticsearch·搜索引擎·阿里云·云计算·全文检索
无所谓^_^10 天前
个人健康管理系统 | Springboot3 | Vue3
java·vue·毕业设计·springboot·课程设计·毕设·前后端分离
在努力的前端小白11 天前
基于FastAPI+LangChain+LangGraph+Agent+多模态的AI Agent 有渔渔具网站
人工智能·langchain·vue·fastapi
sun_weitao11 天前
用 FastAPI + MQTT 做一个 ESP32最小化跑通示例
中间件·vue·fastapi