nginx同域名下部署多个vue项目

一.需求

想要在同一个域名下部署多个前端项目,通过不同url来区分调用相应前端项目。
比如:部署项目a,项目b。想要效果如下。
浏览器输入:http://localhost:8082/a/,展示项目a。
浏览器输入:http://localhost:8082/b/,展示项目b。

二. 本地下载nginx服务器

http://nginx.org/en/download.html

三. nginx常用命令
  1. 进入nginx安装目录,打开终端
  2. 启动服务:start nginx
  3. 停止服务:nginx -s stop nginx -s quit
  4. 修改配置后重新加载生效:nginx -s reload
  5. 查看nginx版本:nginx -v
四. nginx目录介绍

在这里插入图片描述
一般我们部署的时候,会把vue项目发布到html文件夹中,并且修改conf文件夹下的nginx.conf文件。
在这里插入图片描述在这里插入图片描述

五. vue打包配置
  1. 修改vue.config.js,没有这个文件在项目根目录新建一个。
    在这里插入图片描述
    在这里插入图片描述
  2. 修改全局router.js文件
    在这里插入图片描述
六.nginx.conf配置
  server {
        listen       8082;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
       location / {
           root html;
           index  index.html index.htm;
           try_files $uri $uri/ /index.html;
        }
         location /a/ {
           alias html/app/;
           index  index.html index.htm;
           try_files $uri $uri/ /app/index.html;
        }
       location /b/ {
           alias html/pc/;
           index  index.html index.htm;
           try_files $uri $uri/ /pc/index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
6.1 可以单独写一个配置文件,在nginx.conf中引入。这样方便我们后期的维护

在conf目录下新建一个otherconf文件夹。并在此文件夹下新建一个a配置文件。
在这里插入图片描述在这里插入图片描述
a文件

    server {
        listen       8083;
        server_name  localhost;

       location / {
           root html;
           index  index.html index.htm;
           try_files $uri $uri/ /blog/index.html;
        }

         location /a/ {
           alias html/app/;
           index  index.html index.htm;
          try_files $uri $uri/ /app/index.html;
        }

       location /b/ {
           alias html/pc/;
           index  index.html index.htm;
          try_files $uri $uri/ /pc/index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

在nginx.conf文件http模块中引入。

http {
    include       mime.types;
    # 引入所有配置
    include otherconf/*.conf;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
 .... 
}
回答: 在nginx中,可以通过配置来实现一个端口部署多个vue项目。首先,在vue项目vue.config.js文件中,需要配置publicPath参数,以及路由中的base参数,用来指定项目的访问路径。\[1\]接下来,在nginx配置文件nginx.conf中,需要添加对应的location路径,用来匹配请求路径,并将请求转发到对应的项目文件夹下。\[2\]在上传文件到服务器后,需要修改nginx.conf文件,将location路径指向对应的项目文件夹,并重启nginx服务。这样,就可以通过同一个端口访问不同的vue项目了。\[2\] #### 引用[.reference_title] - *1* [Nginx同一端口部署多个vue项目](https://blog.csdn.net/jacob20130/article/details/125368431)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [nginx一个端口部署多个vue项目](https://blog.csdn.net/qq_38255367/article/details/111661353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Nginx一个端口部署多个vue项目](https://blog.csdn.net/outman1023/article/details/117823009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值