Node HTTP proxies with proxied and web sites

I am trying to get websockets to work with node-http-proxy . The difference is that I am using 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 that does not work. Quick check if websockets is working, I get Unexpected response code: 400 from Chrome (works fine if I go directly)

Also doing a few server.on('upgrade',.. checks server.on('upgrade',.. does not start on websocket request

How can I get my proxy to route websites correctly?

I also tried this on node 0.8.23, as well as node 0.10.x (later versions of node have a memory leak problem, but it will not work with 0.8.23)

+8
javascript websocket
source share
1 answer

When you use httpProxy.createServer() , there is no need to handle the upgrade event, because http-proxy processes it automatically. This way your server.on('upgrade', ...) never fails because the HTTP proxy already processes it internally.

The only time you need to execute server.on('upgrade') is when you pass the middleware functions to httpProxy.createServer or if you manually created the server with http.createServer() .

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


However , WebSocket support in http-proxy is currently broken on node v0.10.x due to streams2 (the stream APIs in node core have been completely rewritten in 0.10 ). Also , the latest version of http-proxy (0.10.2) is broken on node v0.8 due to a botched fix for the problem with streams2.

So, you have two options:

  • Wait for http-proxy to rewrite its internal functions to handle streams2.
  • Transition to node v0.8.23 and http-proxy 0.10.1. (At least until # 1.)

(You can install older versions of npm modules by running npm install http-proxy@0.10.1 .)

+2
source

All Articles