Deploying sockjs behind nginx

2,990 views
Skip to first unread message

juha...@gmail.com

unread,
Mar 8, 2013, 9:14:32 AM3/8/13
to soc...@googlegroups.com
Apparently nginx v1.3 added support for proxying websocket connections.
Instructions here http://nginx.org/en/docs/http/websocket.html

I tried following them to deploy sockjs behind nginx, but I failed to get websockets working, only legacy transport protocols.
I did get everything to work with Haproxy, though.

Has anyone got websocket proxying to work with nginx?
Is it still a bad idea to deploy sockjs behind nginx?

Marek Majkowski

unread,
Mar 9, 2013, 10:15:35 AM3/9/13
to juha...@gmail.com, soc...@googlegroups.com
Hi,

On Fri, Mar 8, 2013 at 9:14 AM, <juha...@gmail.com> wrote:
> Apparently nginx v1.3 added support for proxying websocket connections.
> Instructions here http://nginx.org/en/docs/http/websocket.html
>
> I tried following them to deploy sockjs behind nginx, but I failed to get
> websockets working, only legacy transport protocols.
> I did get everything to work with Haproxy, though.
>
> Has anyone got websocket proxying to work with nginx?

I'm afraid I have no experience with sockjs and nginx.

> Is it still a bad idea to deploy sockjs behind nginx?

Not necessarily - but I just don't know if it'll work.

Marek

juha...@gmail.com

unread,
Mar 9, 2013, 11:30:04 AM3/9/13
to soc...@googlegroups.com, juha...@gmail.com
Managed to get it working after recompiling nginx from sources.
Probably just had too old version before.
Here's the config i used. Not really sure if the map directive is what it should be,
but both websocket and legacy transports seem to work fine with this.


http {
    map $http_upgrade $connection_upgrade {
         default upgrade;
         ''      close;
    }

    upstream backend {
        server 127.0.0.1:9800;
    }
    # Only retry if there was a communication error, not a timeout.
    proxy_next_upstream error;

    server {
        server_name  localhost;
        listen 80;
        root   /path/to/myapp/;

        access_log  /var/log/nginx/myapp.access.log;

        # static files
        location / {
        }

        # sockjs
        location /echo {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_redirect off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

theyak

unread,
Jun 15, 2013, 12:29:03 AM6/15/13
to soc...@googlegroups.com, juha...@gmail.com
Worked great for me, thanks!

Serge, might want to add this to the docs. It was far easier than haproxy and you get the nginx's benefits as well.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages