GrepBoard/Manuals
605 manuals ·

Nginx 504 upstream timed out — Tell Which Timeout Fired

·Tested on Ubuntu 24.04 · Nginx 1.24.0 · Python 3.12 (test backend)·Test scope ↓·8 steps

The end of the 504 line in error.log (connecting, reading, sending) names the timeout that fired. Defaults and access-log time variables, with real logs.

upstream timed out (110: Connection timed out) always starts the same way. The end of the line says which timeout fired, and each stage has its own directive. The lines below come from Nginx on port 18621 and a deliberately slow backend on 18622; the leading timestamp, PID and client:/server: fields are cut to …. For 502s (refused or dropped connections) see this post.

[1] Find the upstream timed out lines near the time of the 504 in error.log and read the while … part at the end.

console
# grep 'upstream timed out' /var/log/nginx/error.log | tail -n 5

[2] while connecting to upstream means the TCP connection to the backend was never established. The directive is proxy_connect_timeout. This one went to 192.0.2.1, a documentation address that never answers the SYN.

console
… [error] … upstream timed out (110: Connection timed out) while connecting to upstream, … request: "GET /conn/ HTTP/1.1", upstream: "http://192.0.2.1:80/conn/", …$ curl -s -o /dev/null -w '%{http_code} %{time_total}\n' http://127.0.0.1:18621/conn/504 2.002933

[3] while reading response header from upstream means the connection and request went through, but the backend never started answering. The directive is proxy_read_timeout, and this is the most common form. The backend answered after 5 seconds with proxy_read_timeout 2s.

console
… [error] … upstream timed out (110: Connection timed out) while reading response header from upstream, … request: "GET /slow/ HTTP/1.1", upstream: "http://127.0.0.1:18622/slow/", …$ curl -s -o /dev/null -w '%{http_code} %{time_total}\n' http://127.0.0.1:18621/slow/504 2.002983

[4] while sending request to upstream means the backend stopped accepting the request while Nginx was sending it. The directive is proxy_send_timeout. It was reproduced with a 6 MB POST streamed through proxy_request_buffering off to a backend that never reads the body (proxy_send_timeout 2s).

console
… [error] … upstream timed out (110: Connection timed out) while sending request to upstream, … request: "POST /send/ HTTP/1.1", upstream: "http://127.0.0.1:18622/send/", …$ curl -s -o /dev/null -w '%{http_code} %{time_total}\n' --data-binary @big.bin http://127.0.0.1:18621/send/504 2.005653

[5] All three directives default to 60s. For proxy_connect_timeout the documentation adds that it cannot usually exceed 75 seconds. Look for overrides in the running configuration with nginx -T.

console
# nginx -T 2>/dev/null | grep -E 'proxy_(connect|read|send)_timeout'

[6] To see the stage as numbers, add the upstream time variables to the access log. The documented meanings:

VariableInterval measured
$upstream_connect_timeEstablishing the connection to the backend (includes the SSL handshake)
$upstream_header_timeReceiving the response header
$upstream_response_timeReceiving the whole response
/etc/nginx/nginx.conf
http {   log_format timing '$status $request_time uc=$upstream_connect_time uh=$upstream_header_time ur=$upstream_response_time "$request" $upstream_status';   access_log /var/log/nginx/access.log timing;}

[7] Read the stage from the shape of the line. This is the access log from the tests above; the first line succeeded after 5 seconds with proxy_read_timeout 10s. Times are in seconds.

console
200 5.001 uc=0.001 uh=5.001 ur=5.001 "GET /ok/ HTTP/1.1" 200504 2.002 uc=0.001 uh=- ur=2.003 "GET /slow/ HTTP/1.1" 504504 2.003 uc=- uh=- ur=2.002 "GET /conn/ HTTP/1.1" 504504 2.008 uc=0.000 uh=- ur=2.005 "POST /send/ HTTP/1.1" 504

On the successful line uc is near zero and uh is 5 seconds, so the time went into the backend's processing, not the connection. On the 504 lines uh is - (no header received) and ur equals the configured timeout. uc=- means the connect stage; a recorded uc means reading or sending, which the end of the error.log line tells apart.

[8] Raise a timeout last. Check the backend log, slow queries and outbound API calls for the same time first, and collect the paths where uh is long with awk. If you must raise it, do it in the one location that needs it, not server-wide. And if uc is the long one, no proxy_read_timeout will help.

console
# awk '{ split($4,a,"="); if (a[2]+0 > 3) print }' /var/log/nginx/access.log | head

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

Check it with the HTTP status codes tool ↗ — calculated in your browser.

#nginx#504#timeout