代理配置proxy作用:
1.解決開發環境跨域問題(不用再去配置nginx和host)
2.如果你有單獨的後端開發伺服器API,并希望在同域名下發送API請求,那麼代理某些URL會很有用
下面介紹一下五種經常使用的場景
使用場景一:
請求到 /api/xxx 現在會被代理到請求 http://localhost:3000/api/xxx, 例如 /api/user 現在會被代理到請求 http://localhost:3000/api/user
module.exports = {
//...
devServer: {
proxy: {
'/api': 'http://localhost:3000'
}
}
};
使用場景二
多個字首的路徑,都用一個路徑來代理,使用context屬性
module.exports = {
//...
devServer: {
proxy: [{
context: ['/auth', '/api'],
target: 'http://localhost:3000',
}]
}
};
使用場景三
将url中的 /api替換為空串:
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: {'^/api' : ''}
}
}
}
};
請求到 /api/xxx 現在會被代理到請求 http://localhost:3000/xxx, 例如 /api/user 現在會被代理到請求 http://localhost:3000/user
使用場景四:
如果想要支援https,需要配置。預設情況下,不接受運作在 HTTPS(超文本傳輸安全協定) 上,且使用了無效證書的後端伺服器。
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://other-server.example.com',
secure: false//是否驗證SSL Certs
}
}
}
};
使用場景五:
個人了解:如果想要請求靜态html頁面,繞過代理;對于api請求則保持代理
有時你不想代理所有的請求。可以基于一個函數的傳回值繞過代理。
在函數中你可以通路請求體、響應體和代理選項。必須傳回 false 或路徑,來跳過代理請求。
例如:對于浏覽器請求,你想要提供一個 HTML 頁面,但是對于 API 請求則保持代理。你可以這樣做:
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
bypass: function(req, res, proxyOptions) {
if (req.headers.accept.indexOf('html') !== -1) {
console.log('Skipping proxy for browser request.');
return '/index.html';
}
}
}
}
}
};
解決跨域原理
上面的參數清單中有一個changeOrigin參數, 是一個布爾值, 設定為true, 本地就會虛拟一個伺服器接收你的請求并代你發送該請求,
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
}
}
}
};
vue-cli中proxyTable配置接口位址代理示例
個人了解:這個配置檔案會生成一個代理伺服器,用于連接配接前端請求,向後端api伺服器發送請求
修改 config/index.js
module.exports = {
dev: {
// 靜态資源檔案夾
assetsSubDirectory: 'static',
// 釋出路徑
assetsPublicPath: '/',
// 代理配置表,在這裡可以配置特定的請求代理到對應的API接口
// 使用方法:https://vuejs-templates.github.io/webpack/proxy.html
proxyTable: {
// 例如将'localhost:8080/api/xxx'代理到'https://wangyaxing.cn/api/xxx'
'/api': {
target: 'https://wangyaxing.cn', // 接口的域名
secure: false, // 如果是https接口,需要配置這個參數
changeOrigin: true, // 如果接口跨域,需要進行這個參數配置
},
// 例如将'localhost:8080/img/xxx'代理到'https://cdn.wangyaxing.cn/xxx'
'/img': {
target: 'https://cdn.wangyaxing.cn', // 接口的域名
secure: false, // 如果是https接口,需要配置這個參數
changeOrigin: true, // 如果接口跨域,需要進行這個參數配置
pathRewrite: {'^/img': ''} // pathRewrite 來重寫位址,将字首 '/api' 轉為 '/'。
}
},
// Various Dev Server settings
//host配置通路代理伺服器接受通路的ip位址的範圍
//localhost隻可以在本地通路;如果是0.0.0.0,同一個網段都可以 //通路。
//host: 'localhost', // can be overwritten by process.env.HOST
//可以用process.env.PORT重寫,如果接口被占用,會配置設定一個其他端口
port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
}
更多參數
dev-server 使用了非常強大的 http-proxy-middleware , http-proxy-middleware 基于 http-proxy 實作的,可以檢視 http-proxy 的源碼和文檔:https://github.com/nodejitsu/node-http-proxy 。
target:要通路的後端接口的url字元串
forward:要使用url子產品解析的url字元串
agent:要傳遞給http(s).request的對象(請參閱Node的https代理和http代理對象)
ssl:要傳遞給https.createServer()的對象
ws:true / false,是否代理websockets
xfwd:true / false,添加x-forward标頭
secure:true / false,是否驗證SSL Certs
toProxy:true / false,傳遞絕對URL作為路徑(對代理代理很有用)
prependPath:true / false,預設值:true - 指定是否要将目标的路徑添加到代理路徑
ignorePath:true / false,預設值:false - 指定是否要忽略傳入請求的代理路徑(注意:如果需要,您必須附加/手動)。
localAddress:要為傳出連接配接綁定的本地接口字元串
changeOrigin:true / false,預設值:false - 将主機标頭的原點更改為目标URL