Web Development

Next.js serverless API returns 502 for large Transfer-Encoding: chunked uploads only when behind NGINX reverse proxy

Environment

Environment

Next.js 12 app deployed serverless on self-hosted Node adapter behind NGINX 1.18 reverse proxy on Ubuntu 20.04. Client uploads with Transfer-Encoding: chunked and no Content-Length. NGINX default client_body_buffer_size and proxy_buffering enabled. Small uploads work; >5MB chunked uploads result in 502 with NGINX error logs showing 'upstream prematurely closed connection' while Node app receives partial data.

Symptoms

Symptoms

Large chunked POSTs to /api/upload fail with 502 Bad Gateway from NGINX. Node server logs show request aborted with 'ECONNRESET' after receiving a subset of chunks. Direct access to Node (bypassing NGINX) accepts the same chunked upload successfully.

Observations

Observations

NGINX access logs show short upstream timeouts. Increasing proxy_read_timeout temporarily allows some larger uploads but failures persist. proxy_buffering=on by default; request body buffering may attempt to buffer to disk. NGINX error log includes 'client prematurely closed connection' intermittently.

Already attempted

Already attempted

Tried increasing proxy_buffer_size, proxy_buffers, proxy_read_timeout; added chunked_transfer_encoding off (no effect). Confirmed Node's body-parser accepts streaming chunked requests when tested standalone. Reproduced with curl --chunked and curl --data-binary.

Question

Question

What NGINX and Next.js/Node proxy settings are required to reliably forward large Transfer-Encoding: chunked uploads without NGINX prematurely closing connections or buffering causing issues?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

NGINX buffering request body (proxy_request_buffering on) causing timing/buffer issues with streaming chunked uploads 95%
Insufficient proxy_read_timeout/proxy_send_timeout leading to upstream perceived idle and connection close 65%
NGINX client_max_body_size or tmpfs space for buffered files exhausted causing errors 40%

Summary

Direct Node works, so issue is NGINX proxy behavior with chunked uploads. NGINX by default may buffer request bodies to temporary files and has proxy_buffering that can interact poorly with upstream that expects streaming. For chunked uploads, set proxy_request_buffering off (to stream request to upstream), increase timeouts (proxy_read_timeout/proxy_send_timeout), and ensure client_max_body_size is sufficiently large. Also ensure proxy_buffering is off if upstream streaming is required. These changes avoid NGINX storing the whole body and prevent premature upstream closure.

Suggested Fixes

Diagnostic step 1
In your NGINX location handling /api, set proxy_request_buffering off; also set proxy_buffering off if upstream expects streaming responses.
Diagnostic step 2
Increase proxy_read_timeout and proxy_send_timeout and client_max_body_size to values above your largest upload size.
Diagnostic step 3
Reload NGINX and test with curl --chunked piping a large file; monitor NGINX error log and Node logs for timing or buffer errors.

Continue troubleshooting

Matched from the title, description and tags