Proxying websocket traffic from nginx 1.3.13 to socket.io (without SSL)

OK websockets through nginx working here"

I changed my location section to:

    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://backend;
        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

Now it makes very fast web socket connections. Yipee!


socket.io is a bit sneaky in that it serves up the client script socket.io.js automatically from somewhere in it's node_modules directory.

So what you have to do is tell nginx to pass requests for that location to your node.js as well. In my nginx.conf I have:

    location /chat {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location /socket.io {
        proxy_pass http://backend;
    }

However that only gets me so far as then the websocket connection then fails with a 502 "bad gateway" error.

After that socket.io falls back on xhr-polling which is very, very slow.