J
J
Jahsmine2021-10-13 15:31:27
Python
Jahsmine, 2021-10-13 15:31:27

How to remove websocket disconnect on Nginx?

Good afternoon!

There is the following problem:

There is a backend written in Python Flask - data is sent via a websocket. If you start the server locally or remotely via gunicorn or werkzeug, then everything works well, the connection is not interrupted.

As soon as this whole thing is started through Nginx, web sockets fall off after 30-60 seconds.

To support the connection, we added a "ping" sending every 2 seconds, but this still did not help, web sockets still fall off after 30-60 seconds.

nginx configuration

server {
    listen 213.189.217.11:80;

    location / {
        include proxy_params;
        proxy_redirect off;
        proxy_pass              http://unix:/home/mv-proto-backend/mv-proto-backend.sock;
        proxy_http_version      1.1;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "upgrade";
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;
        proxy_headers_hash_max_size 512;
        proxy_headers_hash_bucket_size 128;
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
        keepalive_timeout 90;
        proxy_cache off;
        proxy_buffering off;
        tcp_nodelay on;
    }
}


Example from postman

6166d18672fc8830400280.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2021-10-13
@Jahsmine

Proxying WebSocket through Nginx does not provide any advantages - the protocol is binary and is proxied as is.
It is better to hang the application on a separate port and allow clients to connect directly.
In the meantime, to properly configure proxying, use the official documentation: WebSocket Proxying
You have everything mixed up in your configuration, you set buffers (why?) then turn off buffering proxy_buffering off;Add extra http headers. You use keepalive_timeout but it's useless here... And so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question