Some requests through your nginx reverse proxy end with “504 Gateway Time-out” after about a minute, and this article is for anyone who runs nginx in front of an application. The upstream (the app server nginx passes each request to) might be Node.js, Gunicorn, PHP-FPM, Apache or another web server. A 504 means nginx gave up waiting for that upstream, and one line in the nginx error log tells you exactly which wait ran out. Read that line first. Then raise the matching timeout only for the slow URL, and keep every outer layer’s timeout longer than the one inside it.
What you’ll accomplish
- Tell from the status code and timing which layer gave up: a CDN, a load balancer, nginx or the app itself.
- Read the nginx error-log line and match it to the one directive that fired.
- Raise a timeout for a single slow endpoint without slowing failure detection for the rest of the site.
- Line up the timeouts from the outside in, so each layer waits longer than the layer behind it.
- Know when the right fix is in the backend, not in nginx.
Prerequisites
You need shell access with sudo on the nginx server, and permission to read /var/log/nginx/error.log and reload nginx. Any supported nginx release works; the timeout directives and their 60-second defaults have not changed in years. Site files live in /etc/nginx/sites-available/ on Debian and Ubuntu, and in /etc/nginx/conf.d/ on RHEL, AlmaLinux and Rocky Linux.
On a cPanel server, nginx usually comes from the ea-nginx package, cPanel’s managed nginx that sits in front of Apache. Its proxy settings live in /etc/nginx/conf.d/includes-optional/cpanel-proxy.conf, which sets the connect, send and read timeouts to 300 seconds. That file belongs to the package and an update replaces it, so leave it alone. cPanel’s ea-nginx notes say custom settings for one domain go in a .conf file under /etc/nginx/conf.d/users/<USER>/<FQDN>/.
How it works
Between the visitor and your code there are usually several layers. Each one starts its own clock when it passes the request inward. Whichever clock runs out first decides the error the visitor sees.
nginx has three clocks for each request it proxies, one per stage:
proxy_connect_timeoutlimits how long nginx waits to open a connection to the upstream. The default is 60 seconds, and the nginx docs note it usually cannot go above 75 seconds.proxy_send_timeoutlimits how long nginx waits while sending the request. It counts the gap between two writes, not the whole upload. The default is 60 seconds.proxy_read_timeoutlimits how long nginx waits for the upstream to send something back. It also counts the gap between two reads. The default is 60 seconds.
When nginx talks to PHP-FPM through fastcgi_pass, the same three clocks are called fastcgi_connect_timeout, fastcgi_send_timeout and fastcgi_read_timeout, with the same defaults. The proxy_* versions have no effect on a FastCGI location.
Because the read clock measures silence, an app that streams a byte every 30 seconds never trips a 60-second proxy_read_timeout. An app that thinks for 61 seconds before sending its first byte always does.
The layers in front of nginx answer with their own codes. Cloudflare does not send 504 when your server is slow. It shows Error 524 once its Proxy Read Timeout runs out. Cloudflare’s documentation now gives 125 seconds for that, while older pages and many guides still say 100 seconds. Only Enterprise plans can change it. An AWS Application Load Balancer sends its own 504 when its idle timeout, 60 seconds by default, expires.
Inside the app, a limit firing usually does not produce a 504 at all. If a Gunicorn sync worker goes silent past its --timeout, or PHP-FPM ends a worker at request_terminate_timeout, the connection closes and nginx returns 502 Bad Gateway. If PHP’s max_execution_time runs out, PHP stops the script with a fatal error and still sends a response, usually a 500 or your app’s own error page.
The rule that follows: each outer timeout must be longer than the one inside it, by at least 5 seconds. Then the app stops a runaway request first and logs which one it was. When an outer layer gives up first, the app keeps working on a request nobody is waiting for.
Step-by-step
1. Time the failure and see who answered. Run this from your own machine against a URL that fails.
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" -D - https://app.example.com/reports/export | grep -iE "^HTTP|^server|^[0-9]{3} "
Example output:
HTTP/2 504
server: nginx
504 60.214s
server: nginx and about 60 seconds point at nginx’s default proxy_read_timeout. A 504 with server: awselb/2.0 comes from an AWS load balancer. Behind Cloudflare the header always reads server: cloudflare, so go by the code: a 524 means Cloudflare gave up first, and a 504 means a layer behind it did. About 300 seconds on a cPanel server matches the proxy timeouts that ea-nginx sets.
2. Read the error-log line. Repeat the request, then look at the newest timeout entries.
sudo grep -E "upstream timed out|prematurely closed|connect\(\) failed" /var/log/nginx/error.log | tail -n 3
Example output:
2026/10/10 09:17:42 [error] 1187#1187: *40311 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 198.51.100.47, server: app.example.com, request: "GET /reports/export?month=2026-09 HTTP/2.0", upstream: "http://10.0.2.15:8000/reports/export?month=2026-09", host: "app.example.com"
The words after “while” tell you which clock fired.
| Phrase in the log | Clock that fired | What it means |
|---|---|---|
upstream timed out ... while connecting to upstream | proxy_connect_timeout | The upstream did not accept a connection in time. It is down, overloaded, or a firewall drops the packets. |
upstream timed out ... while sending request to upstream | proxy_send_timeout | The upstream stopped reading the request, often during a large upload. |
upstream timed out ... while reading response header from upstream | proxy_read_timeout | The upstream got the request but sent nothing back in time. This is the most common case. |
upstream timed out ... while reading upstream | proxy_read_timeout | The upstream went quiet in the middle of the response. The visitor already got a 200 and a cut-off page, so no 504 appears in the access log. |
connect() failed (111: Connection refused) | none | Nothing listens on that port. nginx returns 502, not 504. |
upstream prematurely closed connection | none | The app closed the connection, often because its own timeout killed the worker. nginx returns 502. |
The last two lines are not timeouts in nginx. If you see them, follow the nginx 502 Bad Gateway guide instead.
3. Time the upstream directly. Send the same request straight to the upstream address from the log, skipping nginx. Run this on the nginx server.
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" -H "Host: app.example.com" "http://10.0.2.15:8000/reports/export?month=2026-09"
Example output:
200 83.906s
A 200 after 84 seconds means the app works but needs longer than nginx allows. If the direct request is fast, the upstream is not the slow part. Check the connection between the servers, or whether every app worker was busy when the 504 happened.
4. Find the timeout that applies to that URL. nginx -T prints the full configuration that nginx loaded, including every included file.
sudo nginx -T 2>/dev/null | grep -nE "^# configuration file|location|proxy_pass|_timeout"
Example output:
212:# configuration file /etc/nginx/sites-enabled/app.example.com:
219: location / {
220: proxy_pass http://app_backend;
227: location /reports/ {
228: proxy_pass http://app_backend;
No proxy_read_timeout line means the 60-second default applies. A value set in a location overrides one set in server, which overrides one set in http. Only the block that matches the URL counts, so a timeout raised in location / does nothing for /reports/.
5. Raise the timeout for the slow location only. Pick the value with this rule: the measured time from step 3 plus 25%, rounded up to the next 5 seconds. Then check it stays at least 5 seconds below the next layer out. For the 84-second report, that gives 105 seconds. Behind Cloudflare, the whole chain has to fit under its 125 seconds, and 105 does.
location /reports/ {
proxy_pass http://app_backend;
proxy_connect_timeout 5s;
proxy_read_timeout 105s;
proxy_next_upstream error;
}
Leave the rest of the site on 60 seconds. A long global timeout hides slow pages everywhere and keeps visitors staring at a spinner for longer. A short proxy_connect_timeout of 5 seconds is fine on a private network, so a backend that is down fails fast instead of after a minute.
For PHP-FPM, use the FastCGI names in the matching PHP location, for example fastcgi_read_timeout 105s;.
6. Stop timed-out requests from being retried. That proxy_next_upstream error; line above matters when your upstream block lists more than one server. The default is proxy_next_upstream error timeout, so after a timeout nginx sends the same request to the next server and starts a fresh clock. A GET to a slow report can then wait two full timeouts before the visitor sees anything. nginx does not retry POST requests once they were sent (since nginx 1.9.13), but GET exports and reports are exactly the slow requests that get retried.
Health marking is a separate setting. With the default max_fails=1 and fail_timeout=10s on each server line, one timeout counts as a failed attempt and takes that server out for 10 seconds. Removing timeout from proxy_next_upstream does not change that, because nginx always counts errors and timeouts as failed attempts. To keep one slow report from benching a healthy server, raise the limit on each server line, for example server 10.0.2.15:8000 max_fails=3 fail_timeout=30s;. An upstream block with only one server ignores these settings and never marks it down. If you want to keep retries elsewhere, cap them with proxy_next_upstream_tries 2; or proxy_next_upstream_timeout 30s;.
7. Test and reload.
sudo nginx -t && sudo systemctl reload nginx
Example output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
A reload applies the change without dropping open connections. On a cPanel server, restart nginx with /scripts/restartsrv_nginx instead.
8. Line up the layers from the outside in. Write down each layer’s timeout for the slow path and check that each one is longer than the next.
| Layer | Setting | Example value for /reports/ |
|---|---|---|
| Cloudflare | Proxy Read Timeout | 125 s (Enterprise only to change) |
| Load balancer | idle timeout (AWS default 60 s) | 110 s |
| nginx | proxy_read_timeout in location /reports/ | 105 s |
| App server | Gunicorn --timeout, PHP-FPM request_terminate_timeout, Apache Timeout | 100 s |
The load balancer now needs raising too. Its 60-second default would cut off the report before nginx’s new 105 seconds. Raise only what the slow path needs, and keep the 5-second gaps.
Validate it works
Repeat the request from step 1.
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" "https://app.example.com/reports/export?month=2026-09"
Expected: 200 and a time below your new limit, here under 105 seconds. Next, check that other pages still fail fast. Stop the app for a moment on a test server, and a request to / should return 502 almost at once.
Then count new timeouts over the next day.
sudo grep -c "upstream timed out" /var/log/nginx/error.log
Run it again tomorrow. The count should stay the same, or grow only for URLs you have not fixed yet.
Troubleshooting
Still a 504 at exactly 60 seconds. Another layer still has a 60-second limit. Check the server header from step 1. If it says awselb/2.0 or names another proxy, raise that layer. Behind Cloudflare, test the origin or load balancer address directly to see that header. If it says nginx, run step 4 again; the request probably matches a different location than you expect, for example a regex location that wins over /reports/.
“while connecting to upstream” after only 5 seconds. Your short connect timeout is doing its job. The upstream is not accepting connections, so check that the app is running and that its listen queue is not full. Raising proxy_connect_timeout past 75 seconds usually has no effect anyway.
Random 502s with “upstream prematurely closed connection” after you enable upstream keepalive. Upstream keepalive lets nginx reuse an open connection to the app for several requests. If the app closes idle connections sooner than nginx expects, nginx can send a request down a connection the app has already closed. Set keepalive_timeout in the upstream block below the app’s own value. Node.js, for example, closes idle connections after about 5 seconds by default, so keepalive_timeout 4s; suits a Node.js app. nginx 1.29.7 and later keep upstream connections open by default, so this can appear after an upgrade.
“no live upstreams” in the log after a burst of timeouts. Every server in the upstream block was marked failed by max_fails. Raise max_fails as shown in step 6, then fix the slow requests that triggered it.
Cloudflare 524 even though nginx now waits longer. Cloudflare’s limit is fixed below Enterprise. Make the request finish sooner, or move the long job out of the web request. Serving that one path from a DNS-only hostname that bypasses Cloudflare is the other option.
When this is the wrong approach
- Many URLs time out at once. That is usually a full worker pool or an overloaded database, not one slow endpoint. A longer timeout makes each stuck request hold a worker longer and spreads the outage.
- The work takes minutes by design. Exports, imports and large reports belong in a background job. The page returns at once with a job ID, and the browser checks back until the file is ready.
- The site is WordPress. Use the WordPress 504 Gateway Timeout guide to find the plugin, cron task or query that hung. It uses the same timeout order.
- The connection is a WebSocket or an event stream. Long idle gaps are normal there. Give that location its own long
proxy_read_timeoutand have the app send a heartbeat (a small keep-alive message) more often than the timeout.
The exact wording of every directive is in the nginx proxy module documentation. Cloudflare explains its side in the Error 524 reference.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.