while connecting to upstream
- Directive
proxy_connect_timeout/fastcgi_connect_timeout- Default
60s; the docs note this timeout cannot usually exceed 75 seconds
Fix an nginx 504 Gateway Time-out: read the upstream timed out log line, then tune proxy_read_timeout, fastcgi_read_timeout and the PHP-FPM limits behind it.
In nginx, a 504 Gateway Time-out means nginx waited for the upstream (an app, PHP-FPM or another server) longer than a timeout allows and gave up. The error log says upstream timed out and names the phase. Find which timeout fired, then fix the slow upstream first and raise proxy_read_timeout or fastcgi_read_timeout only if the work is meant to be slow.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
proxy_pass or fastcgi_pass. The general meaning is on 504 Gateway Timeout; a refused or invalid reply is a 502 instead, covered on 502 Bad Gateway in nginx. This page covers the nginx timeouts.while connecting to upstreamproxy_connect_timeout / fastcgi_connect_timeout60s; the docs note this timeout cannot usually exceed 75 secondswhile sending request to upstreamproxy_send_timeout / fastcgi_send_timeout60s, measured between two successive writeswhile reading response header from upstreamproxy_read_timeout / fastcgi_read_timeout60s, measured between two successive reads, not for the whole responseThe nginx source answers with a 504 when an upstream connect, send or read timer expires, and logs upstream timed out followed by the phase it was in. That phase tells you which directive to look at.
# The phase is at the end of the line
sudo grep "upstream timed out" /var/log/nginx/error.log | tail -n 5
# Then time the upstream directly
curl -sS -o /dev/null -w "connect %{time_connect}s total %{time_total}s status %{http_code}\n" http://127.0.0.1:8000/slow-pagelocation /reports/ {
proxy_pass http://127.0.0.1:8000;
proxy_connect_timeout 5s; # fail fast on a dead upstream
proxy_read_timeout 300s; # this endpoint is meant to be slow
}location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_read_timeout 300s;
}log_format timing '$remote_addr "$request" $status '
'rt=$request_time connect=$upstream_connect_time urt=$upstream_response_time';
access_log /var/log/nginx/timing.log timing;| Cause | Where to look | Fix |
|---|---|---|
| The app takes longer than the read timeout | $upstream_response_time in an access log format, and the app's own slow log | Speed up the request, or move the work to a background job. Raise the read timeout only for the one location that needs it |
| PHP stops the script first | request_terminate_timeout (default 0, off) in the pool, and PHP's max_execution_time (default 30) | PHP's max_execution_time stops a script after 30 seconds by default, and request_terminate_timeout kills the worker when set. If a script is meant to run longer, raise the PHP limit and fastcgi_read_timeout together |
| Upstream unreachable at connect time | $upstream_connect_time and firewall rules | Fix the route or the service. The connect timeout is not a place to wait out a dead host |
| Retries make a failure look longer | proxy_next_upstream (default error timeout) | With a group of servers, nginx can try the next one after a timeout, so the visible delay can be several timeouts long |
| Another timer in front of nginx | A CDN or load balancer's own limit | Compare the timeout at each hop; the smaller one fires first |
A 504 is a 5xx response, and repeated 5xx responses can slow Google's crawling; see 5xx server errors. If visitors give up before nginx does, nginx logs 499, covered on 499 Client Closed Request.
Run sudo nginx -t && sudo nginx -s reload.
Request the page that failed and confirm a 2xx. Compare $request_time with the timeout you set; if it is close, the next slow request will fail again.
The upstream timed out line should stop appearing for that URL.
5 questions people ask next, answered from the sources on this page.
Read the error log for upstream timed out and the phase after it. Then speed up the upstream, or raise proxy_read_timeout (or fastcgi_read_timeout for PHP-FPM) on the one location that needs it, and reload nginx.
nginx, acting as a gateway, did not get a timely response from the upstream server. RFC 9110 defines the status, and nginx's connect, send and read timeouts decide when it gives up.
As a visitor, no: the delay is between the site's servers. As the site owner, yes, it is yours to fix, in the upstream's speed or the timeouts between nginx and the upstream.
60 seconds. It applies between two successive read operations, not to the whole response.
A different timer can fire first: the connect timeout, PHP's own limits, or a CDN or load balancer in front of nginx. The phase in the upstream timed out log line shows which.
Run the free audit, then approve the fixes. Every change can be undone.