Vue中配置代理服务器
创建vue.config.js
文件,添加如下配置
devServer:{
proxy:"http://localhost:8888"
}
更多配置
module.exports = {
devServer: {
proxy: {
'/api1': {// 匹配所有以 '/api1'开头的请求路径
target: 'http://localhost:5000',// 代理目标的基础路径
changeOrigin: true,
pathRewrite: {'^/api1': ''}
},
'/api2': {// 匹配所有以 '/api2'开头的请求路径
target: 'http://localhost:5001',// 代理目标的基础路径
changeOrigin: true,
pathRewrite: {'^/api2': ''}
}
}
}
}
- changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000
- changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:8080
- changeOrigin默认值为true