HTTP status code lookup

504 Gateway Timeout: Causes and Fixes

A 504 Gateway Timeout means a proxy, load balancer or CDN forwarded a request to the server behind it and gave up waiting for an answer. The request may still be running on that server; the client just stopped waiting.

504

Gateway Timeout

A gateway or proxy didn't get a response from the server behind it in time.

Look up any other status code →

Common causes

  • A slow database query, a missing index or a locked table.
  • The application calls another API that is slow or down, with no timeout of its own.
  • Long-running work, such as an export or a report, done inside the request instead of in a background job.
  • The application server is overloaded and requests wait in a queue.
  • A network problem between the proxy and the application, so packets never arrive.

Find the slow part first

Raising the timeout hides the problem. Check the application's logs or traces for the request and see where the time goes. Once you know the slow step, fix it with an index, a cache or a background job. Raise the timeout only for requests that genuinely need longer.

Default timeouts

Every layer has its own timeout, so the shortest one between the client and the application decides when a 504 appears.

LayerDefaultSetting
nginx60 secondsproxy_read_timeout, and fastcgi_read_timeout for PHP-FPM
AWS Application Load Balancer60 secondsIdle timeout in the load balancer attributes
Cloudflare100 secondsAfter this, Cloudflare returns its own 524 error

Raise the nginx timeout for one route

location /api/reports/ {
    proxy_pass http://app:3000;
    proxy_read_timeout 300s;
}

Better: answer quickly and finish in the background

For work that takes minutes, return 202 Accepted with a job ID straight away, run the work in a queue, and let the client poll a status URL or receive a webhook when it's done.

Frequently asked questions

Is a 504 my fault or the server's?

It's a server-side problem. As a visitor, wait and try again. As the site owner, look for slow requests behind the proxy.

Does a 504 mean the request failed?

Not necessarily. The upstream may have finished the work after the proxy stopped waiting, so retrying a payment or an order can create a duplicate. Use idempotency keys for requests like these.

What is the difference between 504 and 408?

408 Request Timeout means the server waited too long for the client to send the request. 504 means a proxy waited too long for the upstream server to respond.

More http status code lookup guides

Open the full http status code lookup →

From the blog