具有proxytable和websockets的节点http代理 [英] Node http proxy with proxytable and websockets

查看:104
本文介绍了具有proxytable和websockets的节点http代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将websockets也用于 node-http-proxy 。区别在于我使用的是代理表:

I'm trying to get websockets to also work with node-http-proxy. The difference is i'm using a 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);

我试过:

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

但它似乎不起作用。快速检查一下websockets是否有效显示我从Chrome获得意外的响应代码:400 (如果我直接去的话可以正常工作)

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)

还做了几次检查 server.on('upgrade',.. 不会在websocket请求中触发

Also doing a couple of checks server.on('upgrade',.. doesn't fire on a websocket request

如何让我的代理服务器正确路由websockets?

How can I get my proxy server to route websockets correctly?

我也在节点0.8.23以及节点0.10上尝试了这个。 x(更高版本的节点有内存泄漏问题,但它也无法在0.8.23上运行)

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)

推荐答案

当你使用 httpProxy.createServer(),没有必要处理 upgrade 事件,因为http-proxy会自动处理它。 ,你的 server.on('upgrade',...)永远不会触发,因为http-proxy已在内部处理它。

When you use httpProxy.createServer(), it's not necessary to handle the upgrade event because http-proxy handles it automatically. Thus, your server.on('upgrade', ...) never fires because http-proxy is already handling it internally.

你需要做的唯一一次 server.on('upgrade')是你将中间件函数传递给 httpProxy.createServe的时候r 或者如果您使用 http.createServer()手动创建服务器。

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().

换句话说,websockets应该正常通过您配置中的代理。

In other words, websockets should "just work" through the proxy in your configuration.

但是, http-proxy中的WebSocket支持是目前已破坏 on node v0.10.x 因为streams2(节点核心中的流API 在0.10完全重写)。 同样,由于拙劣修复

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). 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.

所以你有两个选择:


  1. 等待http-proxy重写其内部以应对streams2。

  2. 降级到节点v0.8.23和http -proxy 0.10.1。 (至少在#1发生之前。)

(您可以通过运行 npm来安装旧版本的npm模块安装http-proxy@0.10.1 。)

这篇关于具有proxytable和websockets的节点http代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆