Error Encyclopedia
504

504 Gateway Timeout Error

Fix 504 Gateway Timeout errors. Learn how proxies, load balancers, and CDNs time out waiting for upstream servers.

What Does This Error Mean?

The 504 Gateway Timeout status code means a server acting as a gateway or proxy did not receive a response from the upstream server within the allowed time period.

Common Causes

1

Upstream application server is too slow or overloaded

2

Database queries taking too long to execute

3

External API call timing out

4

Nginx proxy_read_timeout set too low

5

Load balancer health check timeout shorter than expected

6

Server waiting on a deadlocked resource

How to Fix It

Increase proxy timeouts

Configure longer timeout values for gateway/proxy servers.

# Nginx: increase upstream timeout
location /api/ {
  proxy_read_timeout 120s;
  proxy_connect_timeout 30s;
  proxy_send_timeout 30s;
}

# AWS ALB: increase timeout in console
# Target Group → Attributes → Idle timeout: 120 seconds

Optimize slow upstream processes

Profile and optimize the upstream application to respond faster.

// Add request logging to find slow endpoints
app.use((req, res, next) => {
  const start = Date.now()
  res.on("finish", () => {
    const duration = Date.now() - start
    if (duration > 5000) console.warn(`Slow request: ${req.method} ${req.url} (${duration}ms)`)
  })
  next()
})

Use async processing for long operations

Offload long-running tasks to background jobs and return immediately.

app.post("/api/report", async (req, res) => {
  const jobId = await queue.add({ params: req.body })
  res.status(202).json({ jobId, status: "processing" })
  // Client polls GET /api/report/:jobId for result
})

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: