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:

The 504 Gateway Timeout error in Cloudflare indicates a problem specific to your Cloudflare configuration or environment. This guide provides platform-specific solutions.

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 for DNS propagation (up to 5 minutes)
  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: Enable Cloudflare Argo (Paid Feature)
  1. Log into Cloudflare Dashboard
  2. Go to Traffic → Argo
  3. Enable Argo Smart Routing
  4. This uses faster network paths to your origin
  5. Note: This is a paid feature ($5/month + usage)
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. Enable Cloudflare Page Rules for edge caching
  4. Set appropriate Cache-Control headers
  5. Cache API responses with short TTL

Prevention Tips