本地代理

代理服务器参数配置

实现代理服务器功能,可代理到http服务器与https服务器。

在vue-cli下使用

配置config/index.js文件

dev: {
    env: require('./dev.env'),
    port: 7090,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://192.168.199.214:6481',
        changeOrigin: true
      },
    }
}

配置代理服务器处理302重定向的Response

dev: {
    env: require('./dev.env'),
    port: 7090,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://192.168.199.214:6481',
        changeOrigin: true,
        onProxyRes: function(proxyRes, req, res) {
          if ([302].indexOf(proxyRes.statusCode) > -1 && proxyRes.headers.location) {
            var redirect = proxyRes.headers.location;
            console.log('Received code ' + proxyRes.statusCode + ' from API Server for URL - ' + redirect);
            redirect = redirect.replace('http://192.168.199.214:6481', 'http://localhost:7090');
            console.log('Manipulating header location and redirecting to - ' + redirect);
            proxyRes.headers.location = redirect;
          }
        }
      },
    }
}

参考连接

posted @ 2018-12-17 10:30  君寻不惑  阅读(821)  评论(0编辑  收藏  举报