Next.js serverless API returns 502 for large Transfer-Encoding: chunked uploads only when behind NGINX reverse proxy
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
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
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
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
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?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development47% match
NGINX reverse proxy returns 413 for Transfer-Encoding: chunked uploads to Next.js API while direct Node accepts them
Open0 answers - Web Development44% match
NGINX returns 413 for Transfer-Encoding: chunked uploads only when proxying to Node.js over HTTP/2 TLS offload
Solved1 answer - Web Development42% match
NGINX HTTP/2 proxy drops chunked Transfer-Encoding uploads to Next.js API routes — request body never reaches Node
Solved1 answer - Web Development41% match
NGINX returns 413 Request Entity Too Large for chunked uploads only when client uses Transfer-Encoding: chunked
Open1 answer - Networking35% match
Chunked Transfer-Encoding uploads fail with 413 only through TLS-terminating NGINX proxy
Open0 answers