Lorixen.
EN

504 Gateway Timeout on your site: finding the slow link

Short answer: A 504 Gateway Timeout means something in front of your app, such as a load balancer, reverse proxy or CDN, waited for your app to answer and gave up. The app itself is usually still running, just too slow for that request. Find which layer timed out, check what the slow requests have in common, fix the slow work (often a heavy query, an external API call or a big export), and only then adjust timeouts, keeping each layer's limit slightly longer than the one behind it.

What a 504 actually tells you

MDN's reference for the status code describes it plainly: the server, acting as a gateway or proxy, didn't get a response in time from the upstream server it needed to complete the request. It's close to a 502 Bad Gateway, but a 502 means the upstream gave an invalid response or none at all, while a 504 means it simply took too long.

For a typical small business site the chain looks like this:

Visitor → CDN or firewall service → load balancer → web server or reverse proxy (such as Nginx) → app server → database and external APIs

Any of the first four can return a 504. The page your visitor sees often tells you which: CDN error pages are usually branded, load balancers return a plain page, and Nginx shows its own default page unless you've customised it.

Step 1: find the layer that gave up

Each layer has its own timeout, and they're often different:

  • Nginx as a reverse proxy. Its proxy module documentation sets proxy_read_timeout to 60 seconds by default, applied between two successive reads from the upstream rather than to the whole response. proxy_connect_timeout also defaults to 60 seconds.
  • Cloud load balancers. For example, AWS's Application Load Balancer documentation lists an idle timeout of 60 seconds by default.
  • CDNs and firewall services have their own response timeouts, often not adjustable on lower plans. Check your provider's documentation.
  • App servers may have their own worker timeouts, which kill a request before a proxy times out.

Look in each layer's logs for the failed request at the same moment. The layer whose log records the timeout is the one that gave up; the layer behind it is the one that was slow.

Step 2: find what the slow requests share

Before touching timeouts, work out why the app was slow. Group the 504s by:

  • URL or route. Is it always the reports page, the checkout, a search?
  • Time of day. Do they cluster at busy hours or when a scheduled job runs?
  • Customer or data size. Is it one customer with ten years of orders?

Common causes in business apps:

  • A database query without the right index, or one that loads far more rows than needed.
  • An external API, such as a shipping-rate or accounting service, responding slowly with no timeout of its own.
  • A large export or report generated inside the web request.
  • Exhausted database connections, so requests queue waiting for one.
  • Too few app workers for the traffic, so requests wait in line.

Application performance monitoring or tracing makes this much faster; a slow request's breakdown shows where the time went.

Step 3: fix the slow work

Match the fix to the cause:

  • Slow queries: add indexes, limit result sizes, paginate.
  • Slow external APIs: set a short timeout on the outgoing call, cache results where sensible, and show a fallback when the service is slow.
  • Big exports and reports: move them to a background job, then email or notify the user when the file is ready.
  • Connection or worker limits: increase the pool or worker count within what your server can handle, and check for requests that hold connections too long.

Background jobs deserve emphasis. Any request that can legitimately take more than a few seconds should usually not run inside a web request at all.

Step 4: set timeouts deliberately

Once the slow work is fixed, look at timeouts as a whole. A useful principle is that each layer should wait slightly longer than the one behind it:

Layer Example relationship
CDN Longest
Load balancer A little shorter than the CDN
Reverse proxy A little shorter than the load balancer
App server Shorter still
Outgoing API calls and queries Shortest

That way, the innermost layer fails first with a clear error your app can handle and log, instead of an outer layer cutting the connection with a generic 504. The actual numbers depend on your app.

Raising timeouts without fixing the cause is tempting but risky. Long-running requests tie up workers and connections, which can turn one slow page into a site-wide slowdown.

Make 504s easier to diagnose next time

  • Add a request ID that every layer logs, so one request can be followed through all of them.
  • Record request duration in your app logs.
  • Alert on a rising rate of 5xx responses, not just total outages.
  • Customise error pages so visitors see a friendly message and a way to retry or contact you.

A worked example

Consider a hypothetical Gold Coast holiday-rental agency whose owners' portal returned 504 errors whenever an owner opened their yearly statement. The Nginx logs showed upstream timeouts at 60 seconds on that one route, and tracing showed the app building the statement by querying every booking individually. The developer replaced the loop with one indexed query and moved PDF generation to a background job that emails a link. The 504s stopped, and the timeouts were left at their defaults.

FAQ

Is a 504 my host's fault?

Sometimes, if their infrastructure is overloaded, but most 504s come from slow work inside the app.

Do 504s hurt SEO?

Occasional errors are unlikely to matter, but persistent server errors can affect crawling. Fix recurring ones.

Why do I only see 504s sometimes?

Usually because the slow path depends on data size, traffic or a slow external service at that moment.

If your site shows 504 errors and you can't tell where the time goes, ask us to trace it.

Sources

WhatsAppBook a call