使用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:全部当成子项目来部署方便得多,但是老板说主项目必须是访问域名就可以了,不能在根路径下加前缀)。

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

相关推荐
翼龙云_cloud1 小时前
阿里云国际版代理商:OSS自定义域名绑定与HTTPS配置排障教程
数据库·阿里云·https·云计算
皮皮虾❀2 小时前
阿里云代理商采购+超额返点一体化实战:从“单次采购无优惠“到“年度合作解锁最高35%专属返点“的成本优化方案
阿里云·云计算
新中地GIS开发老师1 天前
WebGIS开发学生作品 | 基于无人机影像的烟草检测智能体与数字化管理平台
vue·web·mapbox·webgis
2401_861678621 天前
阿里云远程连接
阿里云·云计算
迪康coolmu1 天前
企业文件外发防泄漏实践:从通道封堵到三层全链路管控
大数据·运维·网络·人工智能·安全·阿里云
南城以南溫暖如初1472 天前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
皮皮虾❀2 天前
阿里云上云迁移服务商选型+官方资质核验+ACE认证团队一体化实战:从“买到假代理”到“官网可查认证服务商承接迁移项目
阿里云·云计算
aXin_ya2 天前
ElementUI (01):Vue2 项目引入 ElementUI使用
elementui·vue
常宇佳3 天前
vue3 @代指src路径设置
前端·typescript·vue