Error Codes Wiki

HTTP 504 Gateway Timeout — Backend Response Timeout Analysis

Critical5xx server error

About HTTP 504 Gateway Timeout

Deep dive into HTTP 504 Gateway Timeout: diagnosing slow backends, proxy timeout tuning, and database query optimization to resolve upstream timeouts. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.

Here are the key things to understand: 504 means the proxy waited too long for a response from the upstream backend. Different from 408 (client timeout) — 504 is between proxy and backend. Default Nginx proxy_read_timeout is 60 seconds. AWS ALB idle timeout default is 60 seconds, can be increased to 4000. Cloudflare has a fixed 100-second timeout on free plans (cannot be changed). Understanding these fundamentals will help you diagnose and resolve this issue more effectively.

The most common reasons this occurs include: Backend processing a slow database query exceeding proxy timeout. Backend server overloaded with too many concurrent requests. Network latency between proxy and backend in different regions. Deadlocked backend threads unable to process new requests. Large file processing or report generation exceeding timeout. DNS timeout resolving the backend hostname. Identifying the root cause is the first step toward finding the right solution.

To resolve this, follow these recommended steps: Identify slow requests: check backend access logs for response times over 30s. Increase proxy timeout: proxy_read_timeout 300s; proxy_connect_timeout 300s; in Nginx. Optimize slow database queries: add indexes, use EXPLAIN to find full table scans. Implement async processing: return 202 Accepted and process in background. Add backend monitoring: track response times, CPU, memory, and connection pool usage. For Cloudflare 504: optimize backend to respond under 100s or upgrade plan for longer timeouts. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.

This article is part of our HTTP Status Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.

Quick Answer

How do I increase the timeout?

In Nginx set proxy_read_timeout. In AWS ALB set idle timeout. Note: Cloudflare free plan has a fixed 100s limit.

Overview

Deep dive into HTTP 504 Gateway Timeout: diagnosing slow backends, proxy timeout tuning, and database query optimization to resolve upstream timeouts.

Key Details

  • 504 means the proxy waited too long for a response from the upstream backend
  • Different from 408 (client timeout) — 504 is between proxy and backend
  • Default Nginx proxy_read_timeout is 60 seconds
  • AWS ALB idle timeout default is 60 seconds, can be increased to 4000
  • Cloudflare has a fixed 100-second timeout on free plans (cannot be changed)

Common Causes

  • Backend processing a slow database query exceeding proxy timeout
  • Backend server overloaded with too many concurrent requests
  • Network latency between proxy and backend in different regions
  • Deadlocked backend threads unable to process new requests
  • Large file processing or report generation exceeding timeout
  • DNS timeout resolving the backend hostname

Steps

  1. 1Identify slow requests: check backend access logs for response times over 30s
  2. 2Increase proxy timeout: proxy_read_timeout 300s; proxy_connect_timeout 300s; in Nginx
  3. 3Optimize slow database queries: add indexes, use EXPLAIN to find full table scans
  4. 4Implement async processing: return 202 Accepted and process in background
  5. 5Add backend monitoring: track response times, CPU, memory, and connection pool usage
  6. 6For Cloudflare 504: optimize backend to respond under 100s or upgrade plan for longer timeouts

Tags

http504gateway-timeoutperformancebackend

More in 5xx Server Error

Frequently Asked Questions

In Nginx set proxy_read_timeout. In AWS ALB set idle timeout. Note: Cloudflare free plan has a fixed 100s limit.