E
E
Evgeny Romashkan2018-10-07 11:58:13
Nginx
Evgeny Romashkan, 2018-10-07 11:58:13

Why does the websocket connection break on the VPS if there is no data exchange for ~ 1 minute?

nginx config:

spoiler
proxy_read_timeout 950s;
server {
        listen 80; 
        server_name 62.109.21.96;
        root  /home/user/ws-chat;

        index index.php;
        # add_header Access-Control-Allow-Origin *;


        location / {
                # add_header Access-Control-Allow-Origin *;
                try_files $uri $uri/ /index.php?$query_string;
        }

        location /ws/ {
                proxy_pass http://localhost:9502;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        location ~* \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Everything works fine locally. What could be causing the problem?

UPD: Thanks for the replies. With a ping every 20 seconds it works well.
In case anyone has this issue with swoole_websocket https://github.com/swoole/swoole-src/issues/2019

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2018-10-07
@EvgeniiR

Libraries like socket.io get around this problem by sending something like a ping (implemented on both sides) every N seconds (for example, half a minute) to check if the connection is alive at the same time (otherwise some providers or buggy routers can also close TCP connections quietly if nothing has been transmitted through them for a long time).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question