Nginx同端口部署多个vue,unapp项目

文章讲述了如何在同一个端口部署PC和App项目,包括Vue项目的打包配置调整、uniapp的manifest.json配置,以及Nginx服务器的location块配置,确保H5路由和资源映射正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

同一个端口部署pc和app端项目,Nginx配置,前端打包配置


解决方案:

  1. 配置pc端vue项目打包配置
    Vue项目 router indexjs 配置
    在这里插入图片描述
  2. 配置uniapp项目打包配置,manifest.json文件添加配置
  "h5": {
    "router": {
      "mode": "hash",
      "base": "./"//改为 /app/
    },
    // pubilcPath的路径要和H5配置中的运行基础路径一致
    "publicPath": "./", //改为 /app/
    "devServer": {
      "disableHostCheck": true,
      //禁止访问本地host文件
      "port": 8088,
      "https": false
    },
    "sdkConfigs": {
      "maps": {
        "qqmap": {
          "key": ""
        }
      }
    },
    "title": "XXXXXXX",
    "optimization": {
      "treeShaking": {
        "enable": true
      }
    }
  }
  1. Nginx config配置
   server {
        listen       3114 default_server;
	listen       [::]:3114 default_server;       
	server_name  _;

	root /mnt/menghai_ds;

        location /pc/ {
	    alias /mnt/menghai_ds/pc/;
            index  index.html index.htm;
		}

        location /app/{
            alias /mnt/menghai_ds/app/;
            index  index.html index.htm;
        }
    }

vue3 + vite
一、项目配置(以项目A和项目B为例)

  1. 修改Vite配置文件(vite.config.js)
    javascript
    // 项目A配置(访问路径:/projectA)
    export default defineConfig({
    base: ‘/projectA/’, // 关键配置:资源基础路径
    plugins: [vue()],
    // 其他配置…
    })

// 项目B配置(访问路径:/projectB)
export default defineConfig({
base: ‘/projectB/’,
plugins: [vue()],
// 其他配置…
})
2. 调整路由配置(router.js)
javascript
// 两个项目均需修改路由历史模式
import { createRouter, createWebHistory } from ‘vue-router’

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), // 自动匹配base配置
routes: [
// 路由定义…
]
})
二、Nginx配置(关键步骤)
nginx
server {
listen 80;
server_name your-domain.com;

主项目配置(假设项目A为主项目)

location / {
root /var/www/projectA/dist;
index index.html;
try_files $uri $uri/ /projectA/index.html; # 路由回退配置
}

子项目配置(项目B)

location /projectB {
alias /var/www/projectB/dist; # 注意使用alias而非root
index index.html;
try_files $uri $uri/ /projectB/index.html;
}

可继续添加更多项目…

location /projectC { … }

}

回答: 在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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值