What a 504 Error Means
Nginx connected to the backend and sent the request, then waited for an answer that did not come. The error log records an upstream timeout while reading the response header. Unlike a 502, the backend is running; it is too slow for this request.
Two directives set the wait: proxy_ for applications behind proxy_, and fastcgi_ for PHP-FPM. Both default to 60 seconds, measured between two successive reads from the backend, not over the whole response. A script that sends output now and then can run longer; one that stays silent for a minute is cut off.
Find the Slow Request
Add timing to the access log. In the http block:
log_format timed '$remote_addr [$time_local] "$request" $status '
'$upstream_response_time $request_time';
access_log /var/log/nginx/timed.log timed;Run sudo nginx -t, then sudo systemctl reload nginx, and list requests that took more than 30 seconds; the last field is the total time:
sudo awk '$NF > 30' /var/log/nginx/timed.log | tailOnce you know the address, reproduce it and watch the backend while it runs. The PHP-FPM slow log, set with request_ and slowlog in the pool file, shows where in the code a script waits.
Usual Causes
- A slow database query: turn on the slow query log of MySQL or MariaDB and add the index that is missing.
- A call to another service without a timeout: an API that does not answer holds the request until Nginx gives up.
- Too few workers: requests wait in a queue behind busy PHP-FPM workers; see 503 Service Unavailable for sizing
pm.max_.children - A server out of CPU or memory: check
uptimeandtop; heavy swap slows everything.
Raise the Timeout as a Stopgap
If one known task needs longer while you fix it, raise the limit only in its location:
location /reports/ {
proxy_pass http://127.0.0.1:3000;
proxy_read_timeout 180s;
}For PHP, inside the PHP location:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 180s;
}The socket path is the Debian and Ubuntu one; on AlmaLinux and Rocky Linux keep the fastcgi_ line you already have. Test with sudo nginx -t and apply with sudo systemctl reload nginx. Reload rather than restart: a failed test leaves the running server as it is, while a restart with a broken file takes the site down. Raising the value for the whole server hides slow pages instead of fixing them.
PHP's Own Limits
- max_
execution_ in php.ini; the default is 30 seconds. On Linux, with the usual PHP builds, it does not count time spent in database queries, system calls, and stream operations, so a script waiting on a slow query can run far longer.time: - request_
terminate_ in the pool file; it kills the worker serving a request that runs longer, and it is off (0) by default.timeout:
When PHP stops a script first, the visitor sees a 500 or 502 instead of a 504. If you raise these limits, keep the Nginx timeout at or above them. The files are /etc/php/8.3/fpm/php.ini and /etc/php/8.3/fpm/pool.d/www.conf on Debian and Ubuntu (use your version), and /etc/php.ini and /etc/php-fpm.d/www.conf on the Red Hat family.
A CDN in Front
A CDN or load balancer has its own limit and stops waiting regardless of your settings. Cloudflare currently documents 125 seconds: if your own timeouts are longer than that, a slow request shows as error 524 instead of your own 504.
Move Long Jobs to the Background
Exports, imports, reports, and bulk mail do not belong in a web request. Let the request create a job and return at once; a queue worker, a cron job, or a systemd service does the work, and the page checks its status later. Then no timeout on any layer applies to the job.
Confirm the Fix
curl -s -o /dev/null -w '%{http_code} %{time_total}\n' https://example.com/reports/Expect 200 and a time well below the limit.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations