Nginx TCP (WebSockets) Timeout / Keepalive Config

2019-01-21 23:33发布

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?

2条回答
萌系小妹纸
2楼-- · 2019-01-22 00:12

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.

查看更多
forever°为你锁心
3楼-- · 2019-01-22 00:20

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 #####;

     ....

         }
查看更多
登录 后发表回答