6

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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

I thought it was supported in the dev branch?!
I ended up using bouncy as you suggested, @Epeli. nginx 1.1.14, much like 1.1.4 doesn't seem to work as noted here: NOTE: At time of writing (v1.1.4), Nginx 1.1 still can't support web sockets.
I received similar error in [email protected].. It's working but it's very slow, which probably means that it's using a different transport mechanism.
I'm seeing the same problem with NGINX 1.2.3 which is supposed to work. Anyone?
I think 1.3.13 is the first version of nginx to support websockets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.