Nginx 504 Gateway Timeout: “upstream timed out (110: Connection timed out)”
Updated 24 November 2026 · By Brent Alexis
Short answer: a 504 from Nginx means your app took longer than Nginx was willing to wait. By default that's 60 seconds, and it counts the time between two pieces of the answer, not the whole request. First find out why the app is slow. If the wait is expected (a big report, an export), raise proxy_read_timeout for that path only, test and reload.
Tested on the official nginx Docker image (Nginx 1.30.8) and Debian 12 (Nginx 1.22.1), with ProxyForge's own Config Generator output and a test app that answers slowly on purpose: 33 checks. The Cloudflare and PHP-FPM notes are from their docs, not lab-tested. How it's tested
- Match the log line
- First: why is the app slow?
- Raise the timeout for one path
- The 60 seconds is between reads, not in total
- “while connecting to upstream”
- 499 in the access log
- Behind Cloudflare: 524, not 504
- PHP-FPM: fastcgi_read_timeout
Match the log line
The browser shows 504 Gateway Time-out. The error log says which wait ran out:
sudo tail -n 20 /var/log/nginx/error.log
With an app that took 70 seconds to answer, Nginx gave up after exactly 60 and logged this, on both versions:
[error] upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: example.com, request: "GET /slow?s=70 HTTP/1.1", upstream: "http://127.0.0.1:3000/slow?s=70", host: "example.com"
The request and upstream parts tell you which page and which app were slow. The end of the first part tells you which wait it was:
| The log says | What happened | The visitor saw |
|---|---|---|
while reading response header from upstream | The app didn't start answering within 60 s | 504 |
while reading upstream | The app started answering, then went quiet for 60 s | A 200 that stops halfway (below) |
while connecting to upstream | Nginx couldn't reach the app at all | 504 (below) |
If the log says Connection refused instead, nothing is listening on the app's port. That's a 502, not a 504: see the 502 guide.
First: why is the app slow?
Raising the timeout makes visitors wait longer, it doesn't make the app faster. Ask the app directly from the server, without Nginx, and time it:
curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' http://127.0.0.1:3000/the-slow-page
Use the port from your proxy_pass line and the path from the log line. If it takes more than 60 seconds here too, the problem is in the app: a slow database query, a call to another service that hangs, or a job that should run in the background. The app's own log is the next place to look.
Raise the timeout for one path
The Config Generator's reverse-proxy output doesn't set any timeouts, so Nginx's defaults apply (60 seconds). This is its location block (HTTPS, Gzip and WebSockets unticked):
location / {
client_max_body_size 10m;
# RHEL/Alma/Rocky: a 502 with "(13: Permission denied)" in the error log is SELinux.
# Allow Nginx to reach the backend: sudo setsebool -P httpd_can_network_connect 1
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Adding one line under proxy_pass turned the 70-second request from a 504 into a 200:
proxy_read_timeout 90s;
Better still, give only the slow part more time. In the lab, a separate block for /reports/ with proxy_read_timeout 300s; passed nginx -t, and every other path kept the 60-second limit (the same 70-second request outside /reports/ was still a 504):
location /reports/ {
proxy_pass http://127.0.0.1:3000;
proxy_read_timeout 300s;
}
A new location block doesn't inherit the proxy_set_header lines from location /, so copy those in too if your app needs them. Then test and reload:
sudo nginx -t && sudo systemctl reload nginx
Avoid setting a huge timeout for the whole site: a stuck app then ties up a connection for every visitor who's waiting.
The 60 seconds is between reads, not in total
Many answers describe proxy_read_timeout as a limit on the whole request. It isn't. Nginx's docs say it applies “between two successive read operations”, and the lab confirmed it:
- An app that sent its headers at once, then one line every 20 seconds for 80 seconds, worked with the default 60-second timeout: a 200 after 80 seconds.
- An app that sent its headers, then went quiet for 70 seconds, was cut off after 60. The visitor got a 200 with half the page, not a 504, because the status had already been sent. The log said
upstream timed out (110: Connection timed out) while reading upstream.
So for a long export or report, an app that sends something every few seconds doesn't need a longer timeout at all. And if users report pages that stop loading halfway with no error, search the log for while reading upstream.
“while connecting to upstream”
When the app's address doesn't answer at all (a firewall dropping packets, a wrong IP, a server that's down), Nginx waits proxy_connect_timeout, also 60 seconds by default. With the lab's backend pointed at an address that never answers and proxy_connect_timeout 5s;, the visitor got a 504 after 5 seconds and the log said:
upstream timed out (110: Connection timed out) while connecting to upstream, client: 127.0.0.1, server: example.com, request: "GET /fast HTTP/1.1", upstream: "http://10.255.255.1:3000/fast", host: "example.com"
Raising this timeout won't help: check the address in proxy_pass and any firewall between Nginx and the app.
499 in the access log
Sometimes the visitor gives up before Nginx does. When the lab's client stopped waiting after 5 seconds on a 20-second page, there was no 504 at all: the access log showed status 499, Nginx's own code for “the client closed the connection”:
"GET /slow?s=20 HTTP/1.1" 499 0
Lots of 499s on the same page point to the same slow app as a 504, just with impatient visitors (or a load balancer in front with a shorter timeout).
Behind Cloudflare: 524, not 504
Not lab-tested. Cloudflare's Error 524 page (checked 24 November 2026) says Cloudflare shows a 524 when your server doesn't send an HTTP response within its default 125-second proxy read timeout, and that only Enterprise plans can raise it. So with Cloudflare in front, raising proxy_read_timeout beyond that doesn't help a request that sends nothing for that long: Cloudflare gives up first. Requests that need longer should send something every few seconds (see between reads) or run as a background job.
PHP-FPM: fastcgi_read_timeout
Not lab-tested. For a PHP site, Nginx talks to PHP-FPM with the fastcgi_ settings, so the matching setting is fastcgi_read_timeout (also 60 seconds by default, per Nginx's docs), placed in the location ~ \.php$ block. PHP has its own limit too: max_execution_time in php.ini or the pool config.
Generate a reverse-proxy Nginx config →
How to use ProxyForge · FAQ: common errors · Guides · What's new · About