504 Gateway Timeout: when the backend is too slow

504 Gateway Timeout means a proxy or gateway gave up waiting for a response from the upstream server behind it.

Check a URL’s status code

504 Gateway Timeout means a proxy, load balancer or CDN waited for the application behind it and gave up before an answer arrived. Unlike 502, the backend didn’t send a broken response; it was simply too slow.

Definition

RFC 9110, section 15.6.5: the server, “while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.” The timeout belongs to the gateway. Nginx waits 60 seconds by default (proxy_read_timeout, fastcgi_read_timeout), and cloud load balancers have their own idle timeouts. Cloudflare reports the same situation as 524.

Common causes

  • Slow database queries: missing indexes, huge joins, locked tables.
  • Pages that call external APIs while rendering, and wait when those APIs are slow.
  • Long-running jobs started by a web request: exports, imports, image processing, sitemaps generated on the fly.
  • Too few workers for the traffic, so requests queue until they time out.
  • Network problems between the proxy and the app server.

SEO impact

Google groups 504 with other server errors (Google’s documentation): Googlebot slows its crawling, indexed URLs survive brief problems, and URLs that keep failing are removed. Slowness hurts even before requests time out. Google’s crawl budget guidance explains that crawl capacity goes down when a site responds slowly or returns errors. Long response times also mean a poor time to first byte for visitors.

How to fix a 504

Find the slow part

# which requests time out?
grep "upstream timed out" /var/log/nginx/error.log | tail

# MySQL: log queries slower than 2 seconds
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;

Make it faster

  • Add database indexes and cache expensive queries (object cache, page cache).
  • Move heavy work to a background queue and let the page poll for the result, the 202 Accepted pattern.
  • Pre-generate large files such as XML sitemaps instead of building them on every request.
  • Set short timeouts on outgoing API calls and fall back to cached data when they fail.

Raise timeouts only as a stopgap

# Nginx
proxy_read_timeout 120s;      # for proxy_pass
fastcgi_read_timeout 120s;    # for PHP-FPM

; PHP-FPM pool and php.ini must allow it too
request_terminate_timeout = 120s
max_execution_time = 120

Timeouts need to line up across layers; otherwise the shortest one still wins.

How to check

The HTTP status checker shows each URL’s status and response time, so you can spot slow pages before they hit the limit, and the page speed test breaks down server response time for a single page.

HTTP/1.1 504 Gateway Time-out
Server: nginx
Content-Type: text/html
Content-Length: 160

Nginx still prints the old “Gateway Time-out” spelling; it’s the same status.

Frequently asked questions

What is the difference between 504 and 502?

A 504 means the upstream server didn’t answer in time. A 502 means it answered with something invalid or dropped the connection. Both come from a proxy or gateway, not the application itself.

Can I fix a 504 by increasing timeouts?

Only temporarily. Longer timeouts hide the slowness from the gateway, but visitors and crawlers still wait. Speed up the slow request or move the work to a background job.

Does a 504 Gateway Timeout affect SEO?

Yes. Google treats it as a server error and crawls less, and URLs that keep timing out can be removed from the index. Slow responses also reduce how much Google crawls in general.