原文首次发表在: Webpack-dev-server的proxy用法
webpack.config.js
中配置下面简单介绍一下五个常常使用的场景
mmodule.exports = { //... devServer: { proxy: { '/api': 'http://localhost:3000' } } };
请求到 /api/xxx
如今会被代理到请求 http://localhost:3000/api/xxx
, 例如 /api/user
如今会被代理到请求 http://localhost:3000/api/user
html
若是你想要代码多个路径代理到同一个target下, 你可使用由一个或多个「具备 context 属性的对象」构成的数组:vue
module.exports = { //... devServer: { proxy: [{ context: ['/auth', '/api'], target: 'http://localhost:3000', }] } };
若是你不想始终传递 /api ,则须要重写路径:node
module.exports = { //... devServer: { proxy: { '/api': { target: 'http://localhost:3000', pathRewrite: {'^/api' : ''} } } } };
请求到 /api/xxx 如今会被代理到请求 http://localhost:3000/xxx
, 例如 /api/user 如今会被代理到请求 http://localhost:3000/user
webpack
默认状况下,不接受运行在 HTTPS 上,且使用了无效证书的后端服务器。若是你想要接受,只要设置 secure: false
就行。修改配置以下:nginx
module.exports = { //... devServer: { proxy: { '/api': { target: 'https://other-server.example.com', secure: false } } } };
有时你不想代理全部的请求。能够基于一个函数的返回值绕过代理。
在函数中你能够访问请求体、响应体和代理选项。必须返回 false 或路径,来跳过代理请求。git
例如:对于浏览器请求,你想要提供一个 HTML 页面,可是对于 API 请求则保持代理。你能够这样作:github
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, 本地就会虚拟一个服务器接收你的请求并代你发送该请求,web
module.exports = { //... devServer: { proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true, } } } };
修改 config/index.js
vue-cli
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: 'localhost', // can be overwritten by process.env.HOST 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/... 。后端
target:要使用url模块解析的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