Web Development

Nginx returns 504 Gateway Timeout only for backend requests over ~45s to my Node API

Environment

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

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

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

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

Diagnostic steps

  1. 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.
  1. 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.
  1. 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.
  1. 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).
  1. 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

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
filipstack Answer
user
Posted 3w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Nginx proxy_read_timeout (or related timeout) shorter than upstream processing time 8%
Upstream (Node) accepts connection but does not keep socket open or sends reset before response 6%
Keepalive/connection reuse causing upstream to see stale socket or hit backlog limits 5%

Summary

504s from Nginx for long requests commonly arise when Nginx hits a timeout (proxy_read_timeout or fastcgi_read_timeout), when upstream closes the socket, when upstream keepalive sockets are exhausted or reused incorrectly, or when intermediate keepalive/idle timeouts (OS or socket-level) drop connections. Also possible: worker_process busy or upstream not reading request body (client_body_timeout). Hypotheses should be verified by comparing timestamps in Nginx and upstream logs, capturing TCP traces, and testing direct upstream connections with matching request characteristics.

Suggested Fixes

Diagnostic step 1
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). Result meaning: 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.
Diagnostic step 2
Check Nginx error log for 'upstream timed out' or related messages and note timestamps; enable debug logging temporarily if logs lack detail (nginx debug). Result meaning: 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.
Diagnostic step 3
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. Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags