vue-router history mode nginx 配置方式

环境情况

  • vue-cli 4.X
  • nginx 1.21.X
  • system: macos

一个域名、多个项目

域名/地址:www.yourDomain.com

项目 A:projectA(最终打包出来的文件夹名称)

项目 B:projectB(最终打包出来的文件夹名称)

想要最终的访问方式:

  • www.yourDomain.com/projectA
  • www.yourDomain.com/projectB

默认 vue-router 会配置如下:

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL, // process.env.BASE_URL 指向的地址可以在 vue.config.js 中的 publicPath 配置,老的版本是 baseUrl
  routes,
});

export default router;

再来看一下 vue.config.js 的配置

// projectA
module.exports = {
  publicPath: "/projectA", //
  outputDir: "projectA",
  // ...
};

// projectB
module.exports = {
  publicPath: "/projectB", //
  outputDir: "projectB",
  // ...
};

接着来配置 nginx

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

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

保存配置,重启 nginx 或者重新加载 nginx 配置

$ nginx restart

# or

$ nginx -s reload

一个域名、一个项目

域名/地址:www.yourDomain.com

项目:project(最终打包出来的文件夹名称)

首先 vue-router 的文件不用动,去修改 vue.config.js 的 publicPath

module.exports = {
  publicPath: "/", //
  outputDir: "project",
  // ...
};

nginx 中配置

location / {    
  root /usr/local/var/www;  # 项目在服务器上的真实路径
  index  index.html index.htm;
  try_files $uri $uri/ @rewrites; 
} 
        
location @rewrites {
  rewrite ^(.*)$ /index.html last;
}

原文链接:https://www.jianshu.com/p/2ddb5983c04f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值