Andrei Varanovich
Andrei Varanovich

Reputation: 502

Nginx times out exactly after 60 seconds

I have an app server on a backend (Rails with Puma) bound to the unix socket this is the relevant part of nginx config

location /live/ {
 proxy_pass http://app; # match the name of upstream directive which is defined above
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_redirect off;
 proxy_set_header Connection 'Upgrade';
 proxy_http_version 1.1;
 chunked_transfer_encoding off;
 send_timeout 300;
 proxy_send_timeout 300;
 keepalive_timeout 7200;
}

SSE events are coming via /live/, so I adjusted ngix config to handle all requests to this route.

The trouble is that connection closes exactly after 60sec. Here is what I see in response headers

Cache-Control:no-cache
Connection:keep-alive
Content-Type:text/event-stream
Date:Mon, 04 Nov 2013 13:41:52 GMT
Server:nginx
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:7065a7bc-3450-4fe2-b60c-33dfa8d41951
X-Runtime:0.010852
X-UA-Compatible:chrome=1
X-XSS-Protection:1; mode=block

So it seems like nginx sets the initial response to close. Why keepalive_timeout does not work here.

In nginx error.log I see

2013/11/04 13:42:52 [error] 3689#0: *9 upstream timed out (110: Connection timed out)   while reading upstream, client: ......., server: myapp.com, request: "GET /live/events HTTP/1.1", upstream: "http://unix:/home/ubuntu/myapp/shared/sockets/puma.sock:/live/events", host: "myapp.com"

Upvotes: 11

Views: 28090

Answers (2)

davidb
davidb

Reputation: 8954

I think you should check your proxy_connect_timeout (default 60s ;) ) and your proxy_read_timeout (default 60s) which may cause this error. You can find the documentation here:

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout

Upvotes: 8

barbolo
barbolo

Reputation: 3887

Just in case anyone else is in trouble with Timeout errors with Nginx + Puma + Rails, the following configuration in Nginx should increase the timeout to 605 seconds (10 minutes and 5 seconds):

server {
    # ... here goes your proxy configuration

    # Avoid 504 HTTP Timeout Errors
    proxy_connect_timeout       605;
    proxy_send_timeout          605;
    proxy_read_timeout          605;
    send_timeout                605;
    keepalive_timeout           605;
}

Upvotes: 19

Related Questions