本地代理

代理服務器參數配置

實現代理服務器功能,可代理到http服務器與https服務器。vue

在vue-cli下使用

配置config/index.js文件git

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

配置代理服務器處理302重定向的Responsegithub

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;
          }
        }
      },
    }
}

參考鏈接

相關文章
相關標籤/搜索