K
K
KeyDel2019-04-07 08:54:42
Nginx
KeyDel, 2019-04-07 08:54:42

How to set up Nginx-Tornado-Websocket binding?

Help setting up Nginx-Tornado-Websocket.
Through Nginx, it turns out to load only statics, there is no switching to Websocket.
Standalone (without Nginx) the Tornado application works fine on port 9001 with these settings. Handlers:
define("port", default=9001)

handlers = [
  (r"/", MainHandler),
  (r"/botsocket", BotSocketHandler),
]

Switching to Websocket (js): Nginx settings (all commented out directives are my vain attempts to find a working configuration):
var url = "ws://IP_сервера/botsocket";
worker_processes 2;

#user                    root;
user                    nginx;

events {
    worker_connections  1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
  
    upstream websocket {
        server IP_сервера:9001;
        #server 127.0.0.1:9001;  
    }

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

    keepalive_timeout 86400;
    proxy_connect_timeout 86400;
    proxy_send_timeout 86400;
    proxy_read_timeout 86400;

    server {
        listen 80;

        location ^~ /static/ {
            root /var/www/python/test/;
            if ($query_string) {
                expires max;
            }
        }

         # WebSocket.
        location /botsocket {
            proxy_http_version 1.1;
            #proxy_set_header Connection "upgrade";
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://websocket;
            #proxy_pass http://websocket/botsocket;
            #proxy_pass http://IP_сервера:9001/botsocket;
            #proxy_pass http://127.0.0.1:9001/botsocket;
        }

         location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://127.0.0.1:9001/;
        }
    }
}

In the current Nginx configuration, in response to a request to switch to Websocket, I get a 403 error.
Changing the user in the Nginx configuration from nginx to root also doesn't change anything.
Help with advice, for the second day I have been fruitlessly digging in Google on other people's configs, which do not work for me.
5ca98e3ec61e3919206240.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question