FULL AI ANALYSIS

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

Generated Oct 26, 2026, 9:23:15 AM

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

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

Upstream Next.js serverless adapter imposes its own body size/time limits leading to aborts when NGINX buffers differently

30%

Suggested fixes

1

Diagnostic step 1

In your NGINX location handling /api, set proxy_request_buffering off; also set proxy_buffering off if upstream expects streaming responses.

2

Diagnostic step 2

Increase proxy_read_timeout and proxy_send_timeout and client_max_body_size to values above your largest upload size.

3

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.

4

Diagnostic step 4

If using temp file buffering, ensure client_body_temp_path has sufficient space; consider disabling buffering only for upload endpoints.