VUE-cli3打包上线到Nginx服务器子目录以及http和https相关设置

一、VUE-cli3打包上线到Nginx服务器子目录

 

VUE-cli3 生成的项目,默认根目录下是没有vue.config.js文件的,需要手动建一个,然后添加publicPath参数即可。

如:

module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? '/m/' : '/',
}

 

Nginx的配置如下:

location /API/ {
    proxy_pass http://localhost:5000;
}

location /m/ {
    root /usr/local/tomcat/webapps/VueWeb/;
    try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
    index  index.html index.htm;
}

location @router {
    rewrite ^.*$ /index.html last;
}

 

二、http和https相关设置

 

发布到线上后,服务器如果同时支持http和https访问的时候,Vue一般是前后端分离的,那么访问接口的地址要和页面地址的协议(http/https)保持一致,要不浏览器会报错。那么Vue使用接口公共地址的地方有两种修改方式:

1、地址不加协议

const APIBasePath = '//www.**.com/API';
const FileBasePath = '//www.**.com';

2、自动加上协议

let protocol = document.location.protocol;
const APIBasePath = protocol+'//www.**.com/API';
const FileBasePath = protocol+'//www.**.com';

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值