Nginx returns 504 Gateway Timeout only for backend requests over ~45s to my Node API
Environment
Ubuntu 20.04 host running Nginx 1.18 as reverse proxy. Node.js 14 app on localhost:3000 (systemd service). Single Nginx instance (no cloud LB) with proxy_pass to http://127.0.0.1:3000; default proxy_read_timeout unless changed.
Symptoms
Requests that take less than ~30s return 200 via Nginx. Requests that perform expensive work or stream for ~45–60s produce HTTP 504 from Nginx; same requests to http://127.0.0.1:3000 (bypassing Nginx) return 200 when allowed to run to completion.
Observations
Nginx access_log entries show 504 status and a request_time roughly matching the timeout. Node process continues running (seen in ps) and writes its own access log entry later when the request completes. No separate fronting load balancer in topology.
Already attempted
Inspected relevant Nginx directives (proxy_read_timeout, proxy_connect_timeout, keepalive settings). Tried increasing proxy_read_timeout in a local test config and reloaded Nginx (no durable change in behavior observed during the test period). Did not change Node application code in these attempts.
Diagnostic steps
- Reproduce the failing request directly against upstream: curl -v --max-time 300 http://127.0.0.1:3000/long-endpoint and compare timing with curl -v --max-time 300 https://your.domain/long-endpoint (through Nginx).
- What the result means: If direct request to upstream completes but Nginx request fails at ~60s, the proxy (Nginx) or its timeouts are implicated. If both fail at same time, upstream likely closes the socket or has its own timeout/issue.
- Check Nginx error log for 'upstream timed out' or related messages and note timestamps; enable debug logging temporarily if logs lack detail (nginx debug).
- What the result means: An 'upstream timed out' entry indicates Nginx stopped waiting for data; absence of such lines means connection may be closed by upstream or by OS-level events — debug logs will show if Nginx sent/received FIN/RST.
- Run tcpdump on loopback or interface while reproducing: sudo tcpdump -i lo -w capture.pcap tcp and inspect with Wireshark or tshark for FIN/RST and timestamps between client, Nginx and upstream.
- What the result means: If a FIN/RST originates from Nginx at timeout, it confirms Nginx closed the socket due to timeout. If a FIN/RST originates from the upstream process earlier, upstream closed the connection. No FIN/RST but connection resets may indicate kernel-level drops.
- Compare Nginx config values: proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout, client_body_timeout and upstream keepalive settings; also check systemd socket timeouts and OS tcp_keepalive_* sysctls.
- What the result means: If any timeout is less than the observed time-to-504, increase it to verify. If timeouts already exceed failure time, look for other causes (upstream behavior or worker limits). Keep changes reversible (edit config backup before reload).
- Temporarily disable keepalive between Nginx and upstream: set keepalive 0 or proxy_http_version 1.0 for that upstream in a test block and reload Nginx; retest failing requests.
- What the result means: If failures stop, keepalive/connection reuse or layer mismatch is implicated. If failures persist, problem likely in timeouts or upstream processing itself. Revert keepalive changes if no improvement.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
What are the most likely causes of selective 504s for long requests proxied via Nginx, what diagnostics can I run to pinpoint whether Nginx or the Node upstream is closing the connection, and what safe, reversible changes should I try first?
Verified community troubleshooting
Start by reproducing the failing call directly against the Node port and through Nginx, then correlate timestamps from the two access logs. Use openssl/curl with --verbose to see the exact timing; run tcpdump on loopback to capture who sends the FIN/RST and at what time. If Nginx sends the FIN at ~60s and its error log says 'upstream timed out', increasing proxy_read_timeout is the expected test (do so on a non-production worker or with a quick rollback plan). If the upstream sends RST, instrument Node (run the same command as the service user) to observe where it blocks or returns an error. These verification steps locate whether Nginx or the Node app closes the socket.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development51% match
NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?
Open1 answer - Web Development40% match
NGINX returns 502 only for long-running Node.js API endpoints while short requests succeed
Open0 answers - Web Development32% match
NGINX returns 413 for Transfer-Encoding: chunked uploads only when proxying to Node.js over HTTP/2 TLS offload
Solved1 answer - Servers29% match
Nginx returns 502 Bad Gateway when proxying to local Node.js upstream intermittently
Open0 answers - Servers29% match
Nginx returns 502 only for long-running API requests
Open0 answers