Checked
2026-11-14
/ 504 Gateway Timeout

504 Gateway Timeout: something behind nginx is too slow

nginx passed the request on and waited. By default it waits 60 seconds between reads, then gives the visitor a 504. Something behind it is slow or stuck: a database query, an outside API, a pool of PHP workers that is full.

Find the request in the log

The error log line reads upstream timed out (110: Connection timed out) while reading response header from upstream. It names the request and the upstream, so you know which page and which app.

grep 'upstream timed out' /var/log/nginx/error.log | tail -n 20

If every page times out, the app or the database is stuck. If one page does, that page does something slow.

Find what is slow

  • top or htop: a process pinned at 100 percent CPU, or a load far above the number of cores, means the machine itself is overloaded.
  • For MySQL or MariaDB, SHOW FULL PROCESSLIST; in the mysql client lists the queries running now and how long each has run. One query running for minutes, or many waiting on a lock, is the cause.
  • For PHP, the PHP-FPM slow log names the script and the function a slow request is stuck in. Set request_slowlog_timeout = 10s and a slowlog file in the pool config, then reload PHP-FPM.
  • In the PHP-FPM log, server reached pm.max_children setting means every worker was busy and new requests waited until nginx gave up.
  • A call to an outside API or a mail server that does not answer holds a worker for as long as its own timeout allows. The slow log shows it as a curl or socket call.

About raising the timeout

proxy_read_timeout, or fastcgi_read_timeout for PHP, sets how long nginx waits; both default to 60 seconds. Raising it is right for a page that is slow on purpose, an export or a report run by staff. For a normal page it only makes visitors wait longer for the same failure.

If you do raise it for PHP, PHP has limits of its own, max_execution_time in php.ini and request_terminate_timeout in the PHP-FPM pool, and either can end the request before nginx does.

What not to do

  • Do not set the timeouts to an hour to make the 504 go away. Each worker stays busy for the whole wait, and under load the site stops answering altogether.
  • Do not restart the database in the middle of a long write. Once you know which query is stuck, KILL with its id in the mysql client ends that one query.
  • Do not buy a bigger server before you know what is slow. A slow query stays slow on more cores.

When to stop and hand it over

Stop when the slow part is the database and you do not know its tables, or when the 504s line up with traffic and the server is at its limits. Our Server Rescue is one incident at a fixed $69, with a same-day start listed at $109. We find the slow part first, change nothing destructive without a backup, and if we cannot fix it, you pay nothing.

/ Questions

Questions people ask

What is the difference between a 502 and a 504?
A 502 means the app refused the connection or sent a broken answer. A 504 means it took the request and did not answer in time.
What is nginx's default timeout?
60 seconds for proxy_read_timeout and for fastcgi_read_timeout. The timer runs between two reads from the app, not for the whole response.
Will more memory or CPU fix it?
Only if the machine is the slow part. A slow query or an outside call that hangs stays slow on a bigger server.
What does fixing a 504 cost?
With us, Server Rescue at $69 for the incident, or $109 for a same-day start. If we cannot fix it, you pay nothing.
/ Sources

Where each statement comes from

Checked against the sources on .

More server help