webpack-dev-server配置了changeOrigin依然无效的解决方案,出现这种情况时因为changeOrigin=true时,底层只会将request-header中的host字段修改,并不会修改Origin字段,同时会在request-header中添加x-forwarded相关字段传递代理前的信息,部分站点会获取x-forwarded信息做来源检测,使用以下配置绕过以上问题:
devServer: { disableHostCheck: true, proxy: { '/api': { target: 'http://www.marklion.cn/', changeOrigin: true, pathRewrite: { '^/api': '/' }, headers:{ //改写Origin,注意结尾不含 / Origin:"http://www.marklion.cn", //改写Referer Referer:"http://www.marklion.cn/", }, "onProxyReq": (request, req, res) => { //移除x-forwarded相关header request.removeHeader('x-forwarded-host'); request.removeHeader('x-forwarded-proto'); request.removeHeader('x-forwarded-port'); request.removeHeader('x-forwarded-for'); } } } }