504 Gateway Timeout — How to Fix It (Nginx, PHP-FPM, Cloudflare, Slow Backend)
If your browser shows:
504 Gateway Timeout
“The server didn’t respond in time.”
your web server waited too long for a response from the backend (PHP, database, or another service).
This guide explains why it happens and exactly how to fix it, even on small VPS servers.
—
✅ Quick Answer (Featured Snippet Optimized)
504 Gateway Timeout occurs when a web server waits too long for a response from PHP-FPM, a database, or another backend service.
Fix it by increasing timeouts, optimizing slow scripts or queries, restarting backend services, and ensuring the server has enough resources.
—
🔍 What Causes a 504 Error?
In simple terms:
Nginx/Cloudflare says:
“I asked the backend for a response… and waited too long.”
Most common causes:
Slow PHP scripts
Database queries taking too long
PHP-FPM overloaded or stuck
No swap / low RAM on VPS
Cloudflare timeout limits
External API calls hanging
—
🛠️ FIX 1: Restart Backend Services (FASTEST TEST)
$$
service php8.2-fpm restart
service nginx restart
$$
If the site loads after this → backend was stuck.
—
🛠️ FIX 2: Increase Nginx Timeouts (Very Common)
Edit your Nginx config:
$$
nano /etc/nginx/nginx.conf
$$
Add or update:
$$
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
fastcgi_read_timeout 300;
$$
Reload Nginx:
$$
nginx -s reload
$$
—
🛠️ FIX 3: Increase PHP Execution Time
Edit php.ini:
$$
max_execution_time = 300
max_input_time = 300
memory_limit = 512M
$$
Restart PHP-FPM:
$$
service php8.2-fpm restart
$$
—
🛠️ FIX 4: Check for Slow Database Queries
If your site uses MySQL/MariaDB:
$$
mysqladmin processlist
$$
Look for:
Queries running too long
Locked tables
👉 Slow DB = 504.
—
🛠️ FIX 5: Add Swap (CRITICAL on 1GB VPS)
504 errors often happen when PHP is killed mid-request.
$$
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo ‘/swapfile none swap sw 0 0’ >> /etc/fstab
$$
—
🛠️ FIX 6: Disable Heavy Plugins / Extensions
For CMS or forums:
One bad plugin can stall PHP.
—
🛠️ FIX 7: Cloudflare Users (IMPORTANT)
Cloudflare has a 100-second timeout on free plans.
If your backend takes longer:
Cloudflare will return 504 if the origin doesn’t respond fast enough.
—
🧪 How to Confirm It’s Fixed
Open site in incognito
Retry the page that failed
Watch logs:
$$
tail -f /var/log/nginx/error.log
tail -f /var/log/php8.2-fpm.log
$$
If requests complete quickly → fixed ✅
—
⚠️ What 504 Is NOT
❌ Not a DNS issue
❌ Not a permissions issue
❌ Not an SSL issue
It’s a slow backend problem.
—
📌 Summary
Error: 504 Gateway Timeout
Cause: Backend too slow or stuck
Fix: Increase timeouts, optimize backend, add swap
Difficulty: Medium
—
💬 Your Turn
Still seeing 504?
Post details below — we’ll isolate the bottleneck 👇
—
📌 Related Fixes You May Need
👉 502 Bad Gateway — Nginx + PHP-FPM Fix
👉 503 Service Unavailable — Server Overload Fix
👉 ERR_TOO_MANY_REDIRECTS — Redirect Loop Fix
👉 Composer Error: Call to Undefined Function putenv()
—
#504error #nginx #phpfpm #cloudflare #webperformance