How to handle Tornado WebSockets using NGINX reverse proxy

869 views
Skip to first unread message

Ravindra Chandrakar

unread,
Jun 13, 2017, 2:25:13 AM6/13/17
to Tornado Web Server
Hello,

I've created a websocket using tornado which is being consumed by angular js code. 
Now I'm setting up a proxy in between using NGINX. With this my websocket stops working. Actually it is searching for a websocket in proxy server. This indicates that the redirection through proxy_pass is not working.

Can someone guide me how to handle this?

Here is the sample of my Nginx proxy configuration.

http

{

......
.....
#Few lines omitted here.
 
map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }


    map $cookie_cookiename $redirectpath {
        default list";
        "1" "list1";
        "2" "list2";
    }

    server {
        listen 80;
        server_name myserver.com;

        root   /var/www/myapplication/;


        proxy_set_header    Host    $host;
        proxy_set_header    X-Real-IP    $remote_addr;
        proxy_set_header    X-Forwarded-Host       $host;
        proxy_set_header    X-Forwarded-Server    $host;
        proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        location / {
            rewrite ^/(.+) /$redirectpath/$1 last;
        }
        location /list/ {
                proxy_pass http://127.0.0.1:8888/;
        }
        location /1/ {
                proxy_pass http://10.0.1.1:8080/;
        }
        location /2/ {
            proxy_pass http://10.0.1.2:8080/;
        }
}



Thanks in advance.

Regards,
Ravindra Chandrakar

HENG

unread,
Jun 15, 2017, 10:14:39 PM6/15/17
to python-tornado
Nginx is not support WebSocket???
Take tornado server expose to your web frontend to take the request.

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--------------------------------------------------------------------
Heng
---------------------------------------------------------------------
--

Japhy Bartlett

unread,
Jun 15, 2017, 11:41:06 PM6/15/17
to python-...@googlegroups.com
I am using this:

location /chatsocket/ {
proxy_pass http://127.0.0.1:{{ project_port }}/chatsocket/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}


inside a server {} block

HENG

unread,
Jun 18, 2017, 11:28:11 PM6/18/17
to python-tornado
I mean that, you would make a test like this:

python your_app.py --address=127.0.0.1 --port=8080

then

make your WebSocket request http://127.0.0.1:8080/chatsocket

This is bypass nginx, It works well in my project

Nginx seems not support WebSocket proxy_pass

Japhy Bartlett

unread,
Jun 19, 2017, 1:06:49 PM6/19/17
to python-...@googlegroups.com
OK just to clarify though-

Ngninx 100% _does_ support forwarding web sockets, and has for some time:



  
Reply all
Reply to author
Forward
0 new messages