欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > devServer changeOrigin不管用

devServer changeOrigin不管用

2025/3/14 19:58:37 来源:https://blog.csdn.net/weixin_38883338/article/details/146242252  浏览:    关键词:devServer changeOrigin不管用

devServer changeOrigin属性是修改后端服务器接收到的 headers 里的 Host 属性

vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,devServer: {proxy: {'/api': {target: 'http://localhost:3000',changeOrigin: false}}}
})

后端 headers

{'x-forwarded-host': '192.168.0.111:8080','x-forwarded-proto': 'http','x-forwarded-port': '8080','x-forwarded-for': '192.168.0.111',referer: 'http://192.168.0.111:8080/',connection: 'close',origin: 'http://192.168.0.111:8080','content-length': '27','content-type': 'application/json','accept-encoding': 'gzip, deflate','accept-language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',accept: 'application/json, text/plain, */*','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0',host: '192.168.0.111:8080'
}

改为 hangeOrigin: true 后 headers

{'x-forwarded-host': '192.168.0.111:8080','x-forwarded-proto': 'http','x-forwarded-port': '8080','x-forwarded-for': '192.168.0.111',referer: 'http://192.168.0.111:8080/',connection: 'close',origin: 'http://192.168.0.111:8080','content-length': '27','content-type': 'application/json','accept-encoding': 'gzip, deflate','accept-language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',accept: 'application/json, text/plain, */*','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0',host: 'localhost:3000'
}

可以看到 host 由 host: '192.168.0.111:8080' 变为后端服务器地址 host: 'localhost:3000'

通过上面的探究,基本可以得出以下结论:

  1. changeOrigin 配置项用来修改 host header,而非 origin header
  2. Host header 用来处理虚拟主机的问题,和跨域无关
  3. 一般情况下,设置代理时可以忽略这个配置项,保持默认即可
  4. 特殊情况下,很可能后端服务就用 host 做校验了,此时根据实际情况去设置

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词