Free auditStart free
GuideError · HTTP status codes

504 Gateway Time-out in nginx

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.

3 min
read
8
sources
Oct 22, 2026
updated
Scaplet Editorial Team
author
The short answer

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.

  1. Why nginx returns a 504
  2. Fix it: slow upstream first, then the timeout
  3. Verify the fix
30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Which timeout fired

Why nginx returns a 504

RFC 9110 defines 504 as the status for a server that, "while acting as a gateway or proxy, did not receive a timely response from an upstream server". nginx is that gateway when you use 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.
Log phase (after `upstream timed out`)

while connecting to upstream

Directive
proxy_connect_timeout / fastcgi_connect_timeout
Default
60s; the docs note this timeout cannot usually exceed 75 seconds
Log phase (after `upstream timed out`)

while sending request to upstream

Directive
proxy_send_timeout / fastcgi_send_timeout
Default
60s, measured between two successive writes
Log phase (after `upstream timed out`)

while reading response header from upstream

Directive
proxy_read_timeout / fastcgi_read_timeout
Default
60s, measured between two successive reads, not for the whole response

The 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.

Find it in the logs
# 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-page
02Fix it on your stack

Fix it: slow upstream first, then the timeout

A longer timeout hides a slow upstream; it does not repair it. The read timeouts count the gap between two reads, so a response that keeps sending data can run longer than 60 seconds without a 504, and one that stays silent for 60 seconds cannot.
Raise the read timeout for one slow endpoint only
location /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
}
The same for PHP-FPM
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 timings to see where the time goes
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;
Causes and fixes
CauseWhere to lookFix
The app takes longer than the read timeout$upstream_response_time in an access log format, and the app's own slow logSpeed 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 firstrequest_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 rulesFix the route or the service. The connect timeout is not a place to wait out a dead host
Retries make a failure look longerproxy_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 nginxA CDN or load balancer's own limitCompare the timeout at each hop; the smaller one fires first
03How it works

Verify the fix

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.

  1. 01

    Reload cleanly

    Run sudo nginx -t && sudo nginx -s reload.

  2. 02

    Repeat the slow request

    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.

  3. 03

    Check the log

    The upstream timed out line should stop appearing for that URL.

Questions

Asked a lot.

5 questions people ask next, answered from the sources on this page.

01How do I fix 504 Gateway Timeout in nginx?

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.

02What does error code 504 mean in 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.

03Is a 504 error my fault?

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.

04What is the default nginx proxy_read_timeout?

60 seconds. It applies between two successive read operations, not to the whole response.

05Why do I get a 504 even after raising proxy_read_timeout?

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.

Sources · 8

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • rfc-editor.org
  • nginx.org
  • github.com
  • php.net
  • google.com
  1. 01RFC 9110: HTTP Semanticsrfc-editor.org · checked 2026-10-25
  2. 02nginx: ngx_http_proxy_modulenginx.org · checked 2026-10-25
  3. 03nginx: ngx_http_fastcgi_modulenginx.org · checked 2026-10-25
  4. 04nginx: ngx_http_upstream_modulenginx.org · checked 2026-10-25
  5. 05nginx source: src/http/ngx_http_upstream.cgithub.com · checked 2026-10-25
  6. 06PHP manual: FPM configurationphp.net · checked 2026-10-25
  7. 07PHP manual: Runtime configuration (max_execution_time)php.net · checked 2026-10-25
  8. 08How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-10-25
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane