How to Fix the 504 Gateway Timeout Error: Nginx, Apache, and Cloudflare Fixes
Google AdSense In-Article Display Placement
Publisher ID: ca-pub-XXXXXXXXXXXXXXXX (Configured in config.php)A 504 Gateway Timeout occurs when the proxy server waited for an answer from the backend PHP server, but the backend took too long to complete its work and exceeded the timeout ceiling (often 60 seconds).
Increase timeout directives across both PHP and your web proxy: 1) In Nginx: set proxy_read_timeout 300; and fastcgi_read_timeout 300;. 2) In PHP: set max_execution_time = 300. 3) Optimize un-indexed slow MySQL queries that freeze the PHP thread.
📋 Guide Table of Contents
1. Problem Diagnosis & Symptoms
Long-running tasks like massive database migrations or unbuffered report generations exceed standard proxy timeouts.
2. Primary Root Causes
- Cause: Unindexed database queries locking tables for over 60 seconds.
3. Step-by-Step Diagnostic Fixes
Increase FastCGI Timeouts in Nginx
Add to nginx.conf or site block:
fastcgi_read_timeout 300;
proxy_read_timeout 300;
proxy_connect_timeout 300;
4. How to Verify the Resolution
- Run operation and verify completion within the extended window.
5. Diagnostic Troubleshooting Matrix
| Symptom Observed | Root Cause | Recommended Action |
|---|---|---|
| 504 occurs at exactly 100 seconds on Cloudflare | Cloudflare free tier limits origin timeout to 100 seconds | Optimize backend query or process via asynchronous background cron queue |
6. Critical Mistakes to Avoid
- ❌ Setting infinite timeouts instead of indexing slow queries.
7. Frequently Asked Questions
❓ Can Cloudflare timeout be increased on the free plan?
No, Cloudflare caps origin HTTP requests at 100 seconds on Free, Pro, and Business tiers.
Official Technical References
Google AdSense In-Article Matched Content Unit