单线程Node.js代理中间件,用于连接,快速和浏览器同步
node.js代理简单。 轻松配置代理中间件连接,快速,浏览器同步等。
由流行的Nodejitsu http代理提供。
TL;DR
代理/ api请求到http://www.example.org
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
可以使用所有http-proxy
选项,以及一些额外的http-proxy-middleware
选项。
提示:将基于名称的虚拟托管网站的选项changeOrigin
设置为true
。
内容列表
- 安装
- 核心概念
- 举个栗子
- 上下文匹配
- 选项
- http-proxy-middleware选项
- http-proxy事件
- http-proxy选项
- 速记
- app.use(path, proxy)
- WebSocket
- 外部Websocket升级
- 工作示例
- Recipes
- 兼容的服务器
- 测试
- 更新日志
- License
Install
- 1
- 1
proxy([context,] config)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- context:确定应将哪些请求代理到目标主机。 (更多关于上下文匹配)
- options.target:目标主机到代理。 (协议+主机)
(http-proxy-middleware
配置选项的完整列表)
proxy(uri [, config])
- 1
- 2
- 1
- 2
更多关于速记配置。
举个栗子
使用express服务器的示例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
Context matching
提供一种替代方式来决定哪些请求应该被代理;如果您无法使用服务器的路径参数来装载代理,或者需要更多的灵活性。
RFC 3986路径用于上下文匹配。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 路径匹配
proxy({...})
- 匹配任意路径,所有的请求都会被代理。proxy('/', {...})
-匹配任意路径,所有的请求都会被代理。proxy('/api', {...})
-匹配所有以/api
开始的路径。
- 多重路径匹配
proxy(['/api', '/ajax', '/someotherpath'], {...})
-
通配符路径匹配
对于细粒度控制,您可以使用通配符匹配。通过micromatch
进行全局模式匹配。访问micromatch
或glob
更多globbing
示例。proxy('**', {...})
匹配任意路径,所有的请求都会被代理。proxy('**/*.html', {...})
匹配所有以.html
结尾的任意路径。proxy('/*.html', {...})
直接匹配绝对路径下的路径。proxy('/api/**/*.html', {...})
匹配在/api
路径下以.html
结尾的请求。proxy(['/api/**', '/ajax/**'], {...})
组合多重路由模式。proxy(['/api/**', '!**/bad.json'], {...})
排除匹配。
- 自定义匹配
为了完全控制,您可以提供一个自定义函数来确定哪些请求应该被代理。
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
选项
http-proxy-middleware选项
-
option.pathRewrite:对象/函数,重写目标的url路径。对象键将被用作RegExp来匹配路径。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
-
option.router:对象/函数,重新定位特定请求的
option.target
。- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- option.logLevel:字符串, [‘debug’, ‘info’, ‘warn’, ‘error’, ‘silent’]. 默认:
'info'
。 -
option.logProvider:函数,修改或者替换日志服务。默认:
'console'
。- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- (已弃用)option.proxyHost:用
option.changeOrigin = true
代替。 -
(已弃用)option.proxyTable:用
option.router
代替。http-proxy 事件
订阅http-proxy事件
-
option.onError:函数,订阅
http-proxy
的error
事件以进行自定义错误处理。- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
-
option.onProxyRes:函数,订阅
http-proxy
的proxyRes
事件。- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
-
option.onProxyReq:函数,订阅
http-proxy
的proxyReq
事件。- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
-
option.onProxyReqWs:函数,订阅
http-proxy
的proxyReqWs
事件。- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
-
option.onOpen:函数,订阅
http-proxy
的open
事件。- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
-
option.onClose:函数,订阅
http-proxy
的close
事件。- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
http-proxy选项
底层http-proxy库提供以下选项。
- option.target:url字符串将与url模块解析
- option.forward:url字符串将与url模块解析
- option.target:传递给http(s)请求的对象(参阅Node https代理和http代理对象)
- option.ssl:传递给https.createServer()的对象
- option.ws:true / false,如果你想要代理websockets
- option.xfwd:true / false,添加x-forward请求头
- option.secure:true / false,如果你想要验证SSL证书
- option.toProxy:true / false,将绝对URL作为
path
(对代理使用代理时很有用) - option.prependPath:true / false,默认:true-指定是否要将目标的路径预置到代理路径
- option.ignorePath:true / false,默认:false-指定是否要忽略传入请求的代理路径(注意:如果需要,您将必须附加/手动)。
- option.localAddress:用于传出连接的本地接口字符串
- option.changeOrigin:true / false,默认值:false - 将主机头的源更改为目标URL
- option.auth:基本认证,即“用户:密码”来计算授权头。
- option.hostRewrite:重写(301/302/307/308)重定向的位置主机名。
- option.autoRewrite:根据请求的主机/端口重写(301/302/307/308)重定向的位置主机/端口。默认值:false。
- option.protocolRewrite:重写位置协议(301/302/307/308)重定向到’http’或’https’。默认值:null。
- option.cookieDomainRewrite:重写set-cookie标头的域。可能的值:
-false
(默认):禁止重写cookie
- 字符串:新域名,比如说cookieDomainRewrite:"new.domain"
。使用cookieDomainRewrite:""
删除域名。
- 对象:域名到新域名的映射,用”*”匹配所有域名。
举个栗子:保持一个域名不变,重写一个域名并且删除其他的:
cookieDomainRewrite: {
"unchanged.domain": "unchanged.domain",
"old.domain": "new.domain",
"*": ""
}
- option.headers:对象,添加请求头。(比如:
{host:'www.example.org'}
) - option.proxyTimeout:超时时间(毫秒)当代理接收不到目标服务器的返回
速记
当不需要详细配置时,请使用简写语法。当使用速记时,上下文和option.target
将被自动配置。如果需要,仍然可以使用选项。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
app.use(path, proxy)
如果要使用服务器的app.usepath参数匹配请求;创建并装载不带
http-proxy-middleware`上下文参数的代理:
- 1
- 1
app.use文档
- express: http://expressjs.com/en/4x/api.html#app.use
- connect:https://github.com/senchalabs/connect#mount-middleware
Websocket
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
外部WebSocket升级
在以前的WebSocket
示例中,http
代理中间件依赖于初始http
请求以便侦听http
升级事件。如果需要在没有初始http
请求的情况下代理WebSockets
,则可以手动预订服务器的http
升级事件。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6