nginx一個(gè)域名配置多個(gè)vue項(xiàng)目

在已有項(xiàng)目一的情況下,不改變項(xiàng)目一的訪問地址,在同一域名的二級(jí)目錄下部署項(xiàng)目二,配置如下:

一、修改vue.js配置

1. 修改vue-router路由配置 src/router/index.js文件

a. 項(xiàng)目一

const router = new VueRouter({
  mode: 'history',
  routes: routes
})

b. 項(xiàng)目二

const router = new VueRouter({
  base: 'jx',
  mode: 'history',
  routes: routes
})

注意圖中標(biāo)記:
image.png

2.注意webpack打包配置 config/index.js

a. 項(xiàng)目一

assetsSubDirectory: 'static',
assetsPublicPath: '/',

b. 項(xiàng)目二

assetsSubDirectory: 'static',
assetsPublicPath: '/jx/',

注意圖中標(biāo)記:
image.png

二、nginx配置

1. nginx-1.15.5\conf\nginx.conf 文件的server配置如下:

# 一個(gè)域名下多個(gè)Vue.js項(xiàng)目的nginx配置
server {
        listen      8001;
        server_name  localhost;
        
        # 項(xiàng)目一
        location / {
            root C:/adanhuan/cy-project/cycxvux/cy;
            try_files $uri $uri/ @router;
            index  index.html  index.htm;
        }
        
        location @router {
            rewrite ^.*$  /index.html last;
        }
        
        # 項(xiàng)目二
        location /jx {
            alias C:/adanhuan/cy-project/cycxvux/jx; 
            try_files $uri $uri/ @router_jx;
            index  index.html  index.htm;   
        }
        
        location @router_jx {
            rewrite ^.*$  /jx/index.html last;
        }
        
        # 接口請(qǐng)求代理,解決跨域
        location /api { 
            proxy_pass http://h5cs.cycxvip.com;
        }

 location /api {        //匹配url中帶有api的,并轉(zhuǎn)發(fā)到http://localhost:8080/api
  rewrite  ^/api/(.*)$ /$1 break;         //利用正則進(jìn)行匹配
  proxy_pass http://localhost:8080;      //轉(zhuǎn)發(fā)的參數(shù)設(shè)定
}
    }

注意圖中標(biāo)記(容易踩坑):
nginx.conf.png

三、重啟nginx后,項(xiàng)目訪問地址如下:

項(xiàng)目一:http://localhost:8001/
項(xiàng)目二:http://localhost:8001/jx/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容