简体   繁体   English

带有代理表和WebSocket的节点HTTP代理

[英]Node http proxy with proxytable and websockets

I'm trying to get websockets to also work with node-http-proxy . 我正在尝试使websocket也可以与node-http-proxy The difference is i'm using a proxytable: 区别在于我使用的是proxytable:

var options = {
router: {
    'a.websterten.com': '127.0.0.1:150',
    'b.websterten.com' : '127.0.0.1:151',
}
};

var server = httpProxy.createServer(options);

I tried: 我试过了:

server.on('upgrade', function (req, socket, head) {
    server.proxy.proxyWebSocketRequest(req, socket, head);
});

But it doesn't seem to work. 但这似乎不起作用。 A quick check to see whether websockets work shows I get Unexpected response code: 400 from Chrome (works fine if I go directly) 快速查看websockets是否正常工作,显示我从Chrome收到Unexpected response code: 400 (如果我直接去的话,效果很好)

Also doing a couple of checks server.on('upgrade',.. doesn't fire on a websocket request 也做了几次检查server.on('upgrade',..不会在websocket请求上触发

How can I get my proxy server to route websockets correctly? 我怎样才能使我的代理服务器正确地路由websocket?

I've also tried this on node 0.8.23 as well as node 0.10.x (the later versions of node have a memory leak issue, but it wont work on 0.8.23 either) 我也在节点0.8.23和节点0.10.x上进行了尝试(节点的更高版本存在内存泄漏问题,但也无法在0.8.23上运行)

When you use httpProxy.createServer() , it's not necessary to handle the upgrade event because http-proxy handles it automatically. 使用httpProxy.createServer() ,无需处理upgrade事件,因为http-proxy会自动处理它。 Thus, your server.on('upgrade', ...) never fires because http-proxy is already handling it internally. 因此,您的server.on('upgrade', ...)永远不会触发,因为http-proxy已经在内部对其进行处理。

The only time you need to do server.on('upgrade') is when you pass middleware functions to httpProxy.createServer or if you've manually created the server with http.createServer() . 唯一需要做的server.on('upgrade')是将中间件函数传递给httpProxy.createServer或者使用http.createServer()手动创建服务器时。

In other words, websockets should "just work" through the proxy in your configuration. 换句话说,websockets应该通过您配置中的代理“正常工作”。


However, WebSocket support in http-proxy is currently broken on node v0.10.x because of streams2 (the stream APIs in node core were completely rewritten in 0.10 ). 但是,由于stream2(节点核心的流API 已完全用0.10重写 ),http-proxy中的WebSocket支持当前在节点v0.10.x上已断开 Also , the most recent release of http-proxy (0.10.2) is broken in node v0.8 because of a botched fix for the streams2 issue. 另外 ,由于对streams2问题的错误修复 ,最新版本的http-proxy(0.10.2)在节点v0.8中被破坏。

So you have two options: 因此,您有两种选择:

  1. Wait for http-proxy to rewrite its internals to cope with streams2. 等待http-proxy重写其内部内容以应对stream2。
  2. Downgrade to node v0.8.23 and http-proxy 0.10.1. 降级到节点v0.8.23和http-proxy 0.10.1。 (At least until #1 happens.) (至少直到发生#1为止。)

(You can install old versions of npm modules by running npm install http-proxy@0.10.1 .) (您可以通过运行npm install http-proxy@0.10.1来安装旧版本的npm模块。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM