Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"websocket connection invalid" when using nginx on node.js server

I'm using Express.js to create a server to which I can connect using web sockets.

Even though it eventually seems to work (that, is connects and passes an event to the client), I initially get an error in Chrome's console:

Unexpected response code: 502

On the backend, the socket.io only logs warn - websocket connection invalid.

However, nginx logs this:

2012/02/12 23:30:03 [error] 25061#0: *81 upstream prematurely closed connection while reading response header from upstream, client: 71.122.117.15, server: www.example.com, request: "GET /socket.io/1/websocket/1378920683898138448 HTTP/1.1", upstream: "http://127.0.0.1:8090/socket.io/1/websocket/1378920683898138448", host: "www.example.com"

Note: I have nginx dev running: nginx version: nginx/1.1.14 so it should support HTTP/1.1.

Also note that if I just use the node.js server without the nginx it works without any warnings.

Finally, here is my nginx config file:

server {
        listen 0.0.0.0:80;
        server_name www.example.com;
        access_log /var/log/nginx/example.com.log;

        location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;

          proxy_pass http://node;
          proxy_redirect off;
        }
}

upstream node {
        server 127.0.0.1:8090;
}

Any help would be greatly appreciated. I tried the fix suggested in this question but that didn't work either.

like image 820
Gezim Avatar asked Feb 13 '12 06:02

Gezim


1 Answers

nginx has some kind of Web Socket support in unstable 1.1 branch only. See Socket.IO wiki.

Afaik there are currently only few stable Node.js based http proxies that support Web Sockets properly.

Check out node-http-proxy (we use this):

https://github.com/nodejitsu/node-http-proxy

and bouncy:

https://github.com/substack/bouncy

Or you can use pure TCP proxy such as HAproxy

Update!

nginx (1.3.13>=) supports websockets out of the box!

http://nginx.org/en/docs/http/websocket.html

like image 60
Epeli Avatar answered Oct 26 '22 16:10

Epeli