FULL AI ANALYSIS

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

Generated Nov 7, 2026, 9:46:16 AM

Analysis 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.

Possible 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%

Nginx worker saturation or insufficient worker_connections causing connection drops under particular load

4%

Suggested fixes

1

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.

2

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.

3

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.

4

Diagnostic step 4

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. Result meaning: 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).

5

Diagnostic step 5

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. Result meaning: 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.