Skip to content

504 Gateway Timeout: Causes and How to Fix It

2 min read

Applies to: Nginx or Apache in front of PHP-FPM or an application, and Cloudflare; tested with Nginx 1.31.6

504 Gateway Timeout means the proxy in front of your application (Nginx, Apache or a load balancer) waited for the backend and gave up. The backend is running, but it is too slow or stuck. In our test, Nginx with a 3-second read timeout returned 504 Gateway Time-out after exactly 3 seconds and logged:

upstream timed out (110: ...) while reading response header from upstream

Nginx waits 60 seconds by default (proxy_read_timeout and fastcgi_read_timeout).

Common causes

  • Long-running PHP scripts: imports, exports, backups, big reports.
  • Slow database queries or locked tables.
  • Calls to external services (payment, shipping, APIs) that hang.
  • An overloaded server: CPU at 100%, memory swapping, or all PHP-FPM workers busy.

1. Find the slow part first

  • Nginx error log: which URLs time out?
  • Server load: top or htop, and free -h.
  • Database: enable the slow query log in MySQL or MariaDB.
  • Measure the page: test TTFB with curl.

2. Move long jobs out of web requests

Imports, backups and reports should run from cron, a queue worker or the command line (for WordPress, WP-CLI), not in a browser request that a proxy will cut off.

3. Raise the timeouts where really needed

Raise them together, for the location that needs it, not for the whole site:

# Nginx + PHP-FPM (inside the location that passes .php files)
fastcgi_read_timeout 300s;

# Nginx as a reverse proxy
proxy_read_timeout 300s;

# PHP (php.ini or MultiPHP INI Editor)
max_execution_time = 300

# PHP-FPM pool, if set
request_terminate_timeout = 300s

Then run sudo nginx -t && sudo systemctl reload nginx and restart PHP-FPM. On CentOS Web Panel with Apache and PHP-FPM: raise the Apache proxy timeout.

Behind Cloudflare

When your server takes too long, Cloudflare shows its own error 524 instead of 504. According to Cloudflare’s documentation, it waits 125 seconds for your server to send a response by default; only Enterprise plans can raise that. Long tasks must therefore run in the background, whatever your own timeouts are.

If the backend answers with an error instead of slowly, you see 502 Bad Gateway. Need a hand? Hire an expert.

Official documentation: Cloudflare error 524.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket