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
Upstream application server is too slow or overloaded
Database queries taking too long to execute
External API call timing out
Nginx proxy_read_timeout set too low
Load balancer health check timeout shorter than expected
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 secondsOptimize 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:
HTTP Status Code Reference
Searchable reference of all HTTP status codes with descriptions and use cases.
REST API Client
Send HTTP requests (GET, POST, PUT, DELETE) and inspect responses from your browser.
Request Builder
Build HTTP requests interactively and generate code snippets in multiple languages.
Related Errors
Other common errors in this category:
401 Unauthorized Error
Learn what a 401 Unauthorized error means, common causes, and how to fix authentication failures in your web applications.
403 Forbidden Error
Learn what 403 Forbidden means, how it differs from 401, and how to fix access denied errors in your applications.
404 Not Found Error
Learn what 404 Not Found means, common causes, and how to fix broken links and missing resources on your website or API.
429 Too Many Requests Error
Learn what 429 Too Many Requests means, how rate limiting works, and how to handle or avoid hitting API rate limits.