35

I am using nginx version: nginx/1.0.12

My nginx.conf looks like this:

#user  nobody;
worker_processes  1;  

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

tcp {

     upstream websockets {
      ## Play! WS location
       server 127.0.0.1:9000;
       check interval=3000 rise=2 fall=5 timeout=1000;
     }    

    server {
        listen 80; 
        listen 8000;
        server_name socket.domain.com;

        tcp_nodelay on; 
        proxy_pass websockets;
        proxy_send_timeout 300;

    }   

     # virtual hosting
     #include /usr/local/nginx/vhosts/*;
}

My application seems to be dropping websocket connnections every 75 sec (or so) which I think is because of Nginx's default keepalive config. How do increase the timeout?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Sameer Segal
  • 21,813
  • 7
  • 42
  • 56

3 Answers3

66

I tried the websocket_*_timeout which are not supported on nginx 1.7.1 (it gives: unknown directive).

However setting a high proxy_*_timeout works:

proxy_connect_timeout 7d;
proxy_send_timeout 7d;
proxy_read_timeout 7d;

7d means 7 days, see official nginx configuration reference

Additionally you probably only have to set the proxy_read_timeout 7d; as that's the one that usually matter unless the server behind the proxy is very slow.

Wernight
  • 36,122
  • 25
  • 118
  • 131
  • I tried the same, and it's working fine... But in Firebug (add on for Firefox), it's giving errors saying - "NetworkError: 404 Not Found - http://localhost/......" and "Firefox can’t establish a connection to the server at ws://localhost/....." – Akash Chandwani Nov 23 '16 at 08:48
  • The `websocket_*_timeout` requires nginx to be compiled with the `nginx_tcp_proxy_module` module from https://github.com/yaoweibin/nginx_tcp_proxy_module – isapir Mar 21 '17 at 20:44
  • 5
    Not sure you really want to set `proxy_connect_timeout` to such a long period as it's not used as a keepalive config. From the manual: `Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.` – duckboy81 Dec 29 '19 at 01:00
  • 3
    `proxy_read_timeout 7d;` should be enough. Don't mess `proxy_connect_timeout` config. It is important. – hurelhuyag Jul 02 '21 at 15:55
  • is this safe to keep `proxy_read_timeout` for `7d`? from performance/resources point of view? – alexanoid Nov 12 '22 at 02:06
8

these brillant guys had the same problem and solved it ....

NGINX to reverse proxy websockets AND enable SSL (wss://)?

also, here in the original repo for that module is more instructions from the module author.

https://github.com/yaoweibin/nginx_tcp_proxy_module/issues/28

it basically amounts to adding websocket_*_timeout instructions in the server directive:

 server {

     ....

     websocket_connect_timeout ######;
     websocket_send_timeout #####;
     websocket_read_timeout #####;

     ....

         }
Community
  • 1
  • 1
sirvon
  • 2,547
  • 1
  • 31
  • 55
0

Other's answer is worked.

Just post offical websocket proxying notes here,
which help us understand more precisely about why and how.



http://nginx.org/en/docs/http/websocket.html

By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds. This timeout can be increased with the proxy_read_timeout directive.

Alternatively, the proxied server can be configured to periodically send WebSocket ping frames to reset the timeout and check if the connection is still alive.



http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout

Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response.

If the proxied server does not transmit anything within this time, the connection is closed.


proxy_read_timeout 1d;
yurenchen
  • 1,897
  • 19
  • 17