我的github
posts - 3243,  comments - 42,  views - 158万

要在Nginx中配置多个Vue项目,您需要为每个项目设置不同的server块,并为每个项目提供不同的静态资源路径。以下是一个基本的配置示例:

复制代码
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    # 第一个Vue项目的server配置
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   /path/to/your/first/vue/project/dist;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
    }
 
    # 第二个Vue项目的server配置
    server {
        listen       80;
        server_name  localhost;
 
        # 注意这里的路径和第一个项目不同
        location / {
            root   /path/to/your/second/vue/project/dist;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
    }
}
复制代码

确保将/path/to/your/first/vue/project/dist/path/to/your/second/vue/project/dist替换为您的Vue项目的实际构建输出目录。

这里的关键点是每个Vue项目都有自己的server块,它们在不同的端口或者不同的URL下提供服务。try_files $uri $uri/ /index.html;指令确保了每个Vue项目都能正确处理前端路由。

参考:百度AI

两种配置方式

如何在nginx配置多个vue项目?第一种是使用不同的端口,既配置多个server即可。第二种是同一个端口,既在一个server里面配置多个vue项目。

参考2:https://blog.csdn.net/BluerCat/article/details/131207177

方法一:使用多个server配置多个vue项目(使用多个端口)

这个方式最简单,将第一份server配置拷贝一下,稍微调整一下就可以了,示例如下:

复制代码
server {
    listen       9001;
    server_name  localhost;
    # 这个是第一个vue项目 页面访问地址 http://ip:9001/one
    location /one {
         root   /home/project/dist/;
         index  index.html index.htm;
         try_files $uri $uri/ /index.html; # 防止页面刷新404
    }
    ... ...
}

server {
    listen       9002;
    server_name  subhost;
    # 这个是第二个vue项目 页面访问地址 http://ip:9002/two
    location  /two {
         root   /home/sub_project/dist/;
         index   index.html;
         try_files $uri $uri/ /index.html; # 防止页面刷新404
    }
    ... ...
}
复制代码

方法二:在一个server配置多个项目(共用一个端口)

多个端口,只需要调整nginx配置,若使用同一个端口,则需要稍微调整一下vue打包配置。

复制代码
server {
    listen       9001;
    server_name  localhost;
    # 这个是第一个vue项目 页面访问地址 http://ip:9001
    location / {
         root   /home/project/dist/;
         index  index.html index.htm;
         try_files $uri $uri/ /index.html;
    }
    # 这个是第二个vue项目 页面访问地址 http://ip:9001/sub
    location  /sub {
         alias   /home/sub_project/dist/;
         index   index.html;
         try_files $uri $uri/ /index.html;
    }
}
复制代码

 

posted on   XiaoNiuFeiTian  阅读(1189)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2023-03-08 java 列表查询
2023-03-08 Cesium.js加载3dTiles格式倾斜摄模型影悬浮空中的解决办法
2023-03-08 Uncaught SyntaxError: Invalid regular expression: /[讗-转貈-劭輴-菘啖�-啵�]/: Range out of order in characte
2021-03-08 ArcGIS Server账户和站点账户的区别
2021-03-08 ArcGIS Server端口号4004是怎么回事。。
2021-03-08 ArcGIS Server SOE(二)
2017-03-08 基于单数码相机的三维摄影测量理论与关键技术研究
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示