HTTPError.net

The fastest way to diagnose, understand, and fix any HTTP status code

Cloudflare

How to Fix 504 Gateway Timeout Error with Cloudflare

Quick Fix

The most common cause of 504 errors in Cloudflare is: Origin server responding too slowly

Quick solution: For Nginx, increase timeout in /etc/nginx/sites-available/your-site:

A 504 Gateway Timeout error in Cloudflare is usually caused by one of a handful of configuration problems. Work through the causes below in order - the first ones are the most common.

Common Causes in Cloudflare

Step-by-Step Solutions

Solution 1: Increase Origin Server Timeout
  1. For Nginx, increase timeout in /etc/nginx/sites-available/your-site:
  2. fastcgi_read_timeout 300; (for PHP-FPM)
  3. proxy_read_timeout 300; (for reverse proxy)
  4. For Apache, edit httpd.conf: Timeout 300
  5. Reload web server: sudo systemctl reload nginx
Solution 2: Optimize Database Queries
  1. Enable slow query log in MySQL: slow_query_log = 1, long_query_time = 2
  2. Check /var/log/mysql/slow-query.log for slow queries
  3. Add indexes to frequently queried columns
  4. Use EXPLAIN to analyze query performance
  5. Consider query caching or Redis
Solution 3: Temporarily Bypass Cloudflare
  1. In Cloudflare DNS, change orange cloud to gray (DNS only)
  2. Wait a few minutes for the change to take effect
  3. Test if site works without Cloudflare
  4. If it works, the issue is origin server performance
  5. Re-enable Cloudflare (orange cloud)
Solution 4: Consider Argo Smart Routing (Paid Add-on)
  1. Argo Smart Routing routes traffic to your origin over less congested network paths
  2. It can reduce latency to the origin but does not fix a slow application
  3. It is a paid add-on - check current pricing in the Cloudflare dashboard before enabling it
Solution 5: Implement Application-Level Caching
  1. Use page caching (WordPress: WP Super Cache, W3 Total Cache)
  2. Implement Redis or Memcached for object caching
  3. Use Cloudflare Cache Rules to cache static or cacheable pages at the edge
  4. Set appropriate Cache-Control headers
  5. Cache API responses with short TTL

Prevention Tips