504 Server Error 📋

504 Gateway Timeout: When the Upstream Takes Too Long

Last updated: June 28, 2026 • 5 min read

✦ The Golden Answer: The 504 Gateway Timeout status code means a server (acting as a gateway or proxy) did not receive a timely response from an upstream server it needed to access. In other words, the upstream server was too slow to respond. This is similar to 502 (Bad Gateway), but 502 means the response was invalid, while 504 means there was no response at all within the timeout period. The problem is almost always on the server side — the upstream server is overloaded, slow, or not responding.

Common Causes of 504 Gateway Timeout

CauseExampleHow to Fix
Upstream server is overloadedHigh traffic, slow database queries, or heavy processing.Scale up resources, optimize code, add caching, or use a load balancer.
Slow backend scriptA PHP/Node.js script takes longer than the gateway's timeout.Increase the timeout in the gateway (e.g., Nginx proxy_read_timeout), or optimize the script.
Network issuesPacket loss, high latency, or routing problems between gateway and upstream.Check network connectivity, use a VPN, or move servers closer together.
DNS resolution delayGateway cannot resolve the upstream hostname quickly.Use a faster DNS or cache IP addresses.
Firewall or security group blockingUpstream server is not reachable due to firewall rules.Check firewall settings and ensure the gateway can reach the upstream.
Upstream server crashed or hungThe upstream process died or is stuck in an infinite loop.Restart the service, check logs, and implement health checks.

Example: Nginx Error Log

Nginx logs a 504 when the upstream times out:

2026/06/28 10:15:23 [error] 1234#1234: *34 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.1.1, server: example.com, request: "GET /slow-script.php HTTP/1.1", upstream: "http://127.0.0.1:8080/slow-script.php", host: "example.com"

This shows that Nginx waited for a response from the upstream but it never came within the timeout period.

How to Fix 504 Errors

  • For visitors: Refresh the page, wait a few minutes, and try again. The issue is often temporary (a spike in traffic or a brief upstream hiccup).
  • For site owners: Increase the timeout in your proxy/gateway configuration (e.g., proxy_read_timeout in Nginx). Optimize your backend code to respond faster. Add caching (e.g., Redis, Varnish) to reduce load. Check server resources (CPU, memory) and upgrade if needed. Use a CDN to offload traffic.

Preventing 504 Errors

  • Set appropriate timeouts – long enough for normal requests, but not too long to cause cascading failures.
  • Implement retries – configure your proxy to retry the request to a different upstream if one fails.
  • Use a queue – offload heavy processing to a background queue to keep the response fast.
  • Monitor and alert – track response times and set alerts when they exceed thresholds.

Frequently Asked Questions

What's the difference between 504 and 502?

502 Bad Gateway means the upstream server sent back an invalid response (e.g., malformed data). 504 Gateway Timeout means the upstream server didn't respond at all within the timeout.

Is a 504 error my fault?

As a visitor, no. It's a server-side issue. As a site owner, it's your responsibility to address the upstream performance or timeout configuration.

Can a CDN cause a 504 error?

Yes. If your CDN cannot reach your origin server within its timeout, it may return a 504 error. Check your CDN settings and origin server responsiveness.