vue使用代理proxy配置,解决跨域

跨域的解决办法有很多,比如script标签 、jsonp、后端设置cros等等...,但是我这里讲的是webpack配置vue 的 proxyTable解决跨域。

访问接口:https://fs.52yiyantang.com/index.php?g=Fs&m=FsApi&a=index

那么在ProxyTable中具体配置如下:

proxyTable: {
      '/api': {
        target: 'https://fs.52yiyantang.com/', // 你要代理的域名和端口号,要加上http
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'   //或者 '^/api': ''
        }
      }
    },

api配置部分:

axios请求页面代码:(this.GLOBAL.API_TEST2是我个人在mian.js中配置的

 

注:proxy只是解决了开发环境的跨域,线上依然会产生跨域的问题。上线时需要配置nginx!

 

2. npm run build 把 dist 放线上后解决:

nginx 的 配置文件 xx.conf 的 server {} 里加如下:

location /api/ {
        # 把 /api 路径下的请求转发给真正的后端服务器
        proxy_pass http://xx.xx.xx.xx:5568;

        # 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是localhost:8080
        proxy_set_header Host $http_host;

        # 把cookie中的path部分从/api替换成/service
        proxy_cookie_path /api /;

        # 把cookie的path部分从localhost:8080替换成your.domain.name
        proxy_cookie_domain localhost:80 http://xx.xx.xx.xx:5568;
    }

重新启动一下 nginx 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值