504 Gateway Timeout-causes and fixes
Fix 504 Gateway Timeout errors. Slow PHP scripts, long database queries, resource overload, upstream server issues.
On this page
HTTP 504 Gateway Timeout means the upstream server (web server, proxy, or gateway) took too long to respond and gave up waiting. Unlike 503 (server overloaded), 504 means the server is reachable but slow or hung.
Common causes
- Slow PHP script: Script takes 5+ minutes, exceeds timeout (usually 30 seconds)
- Slow database query: Query locks tables or scans millions of rows
- Long-running process: Backup, import, or cron job consuming resources
- Resource exhaustion: CPU/memory maxed out, processes can't respond
- Proxy timeout: CloudFlare or reverse proxy timeout too short
- External API call: Your site calls slow third-party API that doesn't respond
- DNS timeout: DNS lookup taking too long
Shared hosting - what you can do
- Disable problematic plugins: Via FTP, rename /wp-content/plugins/ to /wp-content/plugins-old, test site
- Increase PHP timeout: Add to wp-config.php:
define('WP_MEMORY_LIMIT', '256M'); - Check for scheduled tasks: WordPress crons or scheduled backups running = server overload
- Optimize images: Large uncompressed images slow page loads
- Clear cache: Delete /wp-content/cache/ folder if cache plugin is stuck
- Contact support if persistent: May indicate server-level issues needing investigation
Cloud VPS - investigate and fix
Check system resources
top # shows CPU and memory usage
df -h # shows disk usage
ps aux | grep php # shows PHP processes
Find slow queries (MySQL)
sudo tail -100 /var/log/mysql/slow.log
Check PHP-FPM status
sudo systemctl status php-fpm
sudo systemctl restart php-fpm
Increase PHP timeout
Edit /etc/php/*/fpm/php.ini:
max_execution_time = 300 # increase from default 30
Slow PHP scripts
- WordPress: Disable plugins one by one to find the culprit
- Large imports: Import 10,000+ records? Increase timeout and memory limit
- Backup scripts: Running during peak traffic? Schedule backups at 3am
- Cron jobs: Long-running cron (cache clearing, cleanup) causing 504s?
Slow database queries
- Add indexes: Missing indexes on frequently-queried columns
- Analyze query: Use EXPLAIN to find bottlenecks
- Split queries: Instead of SELECT * from 1M rows, paginate (LIMIT 100)
- Archive old data: Move old posts to archive table if database is huge
504 Troubleshooting checklist
- ☐ Clear browser cache (Ctrl+Shift+Delete)
- ☐ Disable plugins (rename /wp-content/plugins)
- ☐ Check task scheduler - are backups/crons running?
- ☐ (VPS) Check system resources: top, df -h, free -h
- ☐ (VPS) Check PHP timeout: php.ini max_execution_time
- ☐ (VPS) Restart PHP-FPM: systemctl restart php-fpm
- ☐ (VPS) Check slow query log for database issues
- ☐ Check CloudFlare settings (if using) - increase timeout
- ☐ Check PHP memory limit - increase if hitting limit
- ☐ Enable debug logging to see which script is slow
504: Server took too long to respond
503: Server is busy/overloaded (usually temporary)
502: Server crashed or disconnected (worse than 504)
Related: 502 Bad Gateway | 503 Service Unavailable | MySQL tuning
Still troubleshooting?
Use UnderHost tools for quick checks, or open a support ticket when the issue needs account or server access.





















