Web Development

NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?

Environment

Environment

Nginx TLS-terminating reverse proxy on Ubuntu 20.04; Node.js Express app behind it on same host using proxy_pass to http://127.0.0.1:4000/. Clients upload 100–500 MB files over mobile/unstable links.

Symptoms

Symptoms

Upload requests hang then Nginx returns 504. When the same client uploads directly to Node (bypassing Nginx), upload finishes. Short uploads succeed through proxy. Nginx logs show 'upstream timed out (110: Connection timed out) while reading response header from upstream'.

Observations

Observations

proxy_read_timeout and proxy_send_timeout are at default or 60s. client_body_buffer_size small. Node receives partial data for timed-out requests (application-level logs show incomplete multipart parts).

Already attempted

Already attempted

Increased proxy_read_timeout and client_body_buffer_size; moved client_body_temp_path to larger partition; no consistent improvement across mobile clients.

Diagnostic steps

Diagnostic steps

  1. Reproduce with access logging at upstream and proxy levels: log request start/complete timestamps in Node and enable Nginx logs with $request_time and $upstream_response_time for the same request.
  • What the result means: If Nginx request_time >> Node processing time and Node shows incomplete body receipts, the timeout is due to slow client upload and Nginx reading body. If Nginx times out after Node completed processing, focus on Nginx→upstream response path or buffering.
  1. Check whether Nginx buffers client body to disk by inspecting client_body_temp_path during reproductions (ls -l /var/lib/nginx/body) and set client_body_buffer_size larger for a short test.
  • What the result means: If temp files appear and grow during uploads and disk I/O spikes, buffering to disk is active and slow disk or small buffer sizes could be contributing. If no temp files appear, Nginx may be streaming directly to upstream.
  1. Temporarily increase proxy_read_timeout and proxy_send_timeout for a test location and reproduce from a slow client; also test direct Node path concurrently.
  • What the result means: If increasing timeouts prevents 504s, the issue is timeouts against slow clients or long upstream processing; if 504s persist despite long timeouts, investigate network resets or upstream crashes.
Confirmed fixes

Confirmed fixes

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

Question

Question

Which diagnostic steps confirm whether the timeout is occurring reading the request body, waiting for the upstream response, or due to buffering behavior, and what low-risk changes can be tested first?

COMMUNITY RESPONSES

Verified community troubleshooting

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

A pragmatic path: correlate timestamps in Node access logs with Nginx $request_time and $upstream_response_time. If Nginx spends most time receiving the client body (and temp files appear), the proxy is waiting on a slow client and you should raise client and proxy timeouts or increase client_body_buffer_size to reduce disk buffering. If Node finishes but Nginx still returns 504, increase proxy_read_timeout for testing and watch kernel dmesg for resets. Verify fixes by reproducing with the slow client and confirming Nginx now forwards the complete request and Node finishes processing.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Nginx times out waiting for slow client body or upstream response (proxy_read_timeout/proxy_send_timeout too low). 7%
Nginx request-body buffering writes to temp disk and stalls due to slow disk or small client_body_buffer_size. 5%
Node blocks while processing multipart stream because of synchronous disk operations or memory pressure causing slow response. 5%

Summary

504 indicates Nginx gave up waiting for upstream to respond. For uploads, Nginx may buffer request body to disk before forwarding, or timeouts may fire while waiting for slow client body arrival. Key checks: whether Nginx is buffering body (client_body_in_file_only), whether timeouts occur before Node finishes processing, and whether Nginx is attempting to read response headers while still receiving body. The different outcomes point to client slowness, proxy buffering, or upstream processing delays.

Suggested Fixes

Diagnostic step 1
Reproduce with access logging at upstream and proxy levels: log request start/complete timestamps in Node and enable Nginx logs with $request_time and $upstream_response_time for the same request. Result meaning: If Nginx request_time >> Node processing time and Node shows incomplete body receipts, the timeout is due to slow client upload and Nginx reading body. If Nginx times out after Node completed processing, focus on Nginx→upstream response path or buffering.
Diagnostic step 2
Check whether Nginx buffers client body to disk by inspecting client_body_temp_path during reproductions (ls -l /var/lib/nginx/body) and set client_body_buffer_size larger for a short test. Result meaning: If temp files appear and grow during uploads and disk I/O spikes, buffering to disk is active and slow disk or small buffer sizes could be contributing. If no temp files appear, Nginx may be streaming directly to upstream.
Diagnostic step 3
Temporarily increase proxy_read_timeout and proxy_send_timeout for a test location and reproduce from a slow client; also test direct Node path concurrently. Result meaning: If increasing timeouts prevents 504s, the issue is timeouts against slow clients or long upstream processing; if 504s persist despite long timeouts, investigate network resets or upstream crashes.

Continue troubleshooting

Matched from the title, description and tags