Web Development

NGINX reverse proxy returns 413 for Transfer-Encoding: chunked uploads to Next.js API while direct Node accepts them

liamlogic
1mo ago
Environment

Environment

Next.js 13 app using custom server (Node.js 18), fronted by NGINX 1.20 as reverse proxy with default proxy_buffering settings, client_max_body_size set to 50M, hosted on Ubuntu 22.04.

Symptoms

Symptoms

Large multipart uploads sent with Transfer-Encoding: chunked receive HTTP 413 Request Entity Too Large from NGINX. The same upload sent directly to the Node/Next server bypassing NGINX succeeds. Short or non-chunked uploads through NGINX work fine.

Observations

Observations

NGINX access/error logs show 413 and 'client intended to send too large body'. client_max_body_size is already > file size. Turning off buffering at the upstream level was attempted with no immediate effect until NGINX restart. When proxy_request_buffering is off, uploads stream to upstream and the issue disappears.

Already attempted

Already attempted

Increased client_max_body_size in nginx.conf (no effect), set proxy_buffering off in location block (fixed after restart), tested with curl -H 'Transfer-Encoding: chunked' confirming behavior, checked upstream Next.js server logs (no 413).

Question

Question

Why does NGINX return 413 for chunked uploads even when client_max_body_size is large, and what is the safe, production-ready way to accept chunked uploads through NGINX to Next.js endpoints?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

NGINX request buffering/size-check behavior with chunked Transfer-Encoding causes size check to fail and trigger 413 95%
client_max_body_size not applied to the specific location block due to include/overrides or vhost mismatch 40%
Upstream returning 413 but being masked as NGINX error (less likely given upstream logs) 20%

Summary

NGINX may buffer request bodies by default; for chunked Transfer-Encoding it needs to accumulate the body to measure size unless proxy_request_buffering is disabled. If buffering is enabled and NGINX determines the body exceeds client_max_body_size (or buffer settings disallow chunked streaming), it returns 413. Disabling request buffering streams chunks straight to the upstream (Next.js), enabling uploads without size checks by NGINX. Proper config is to set proxy_request_buffering off in the upload endpoint location or use client_body_in_file_only to stream to disk, then proxy_pass.

Suggested Fixes

Diagnostic step 1
Immediate test: in your upload location add proxy_request_buffering off; reload nginx and retry chunked upload. Verify success.
Diagnostic step 2
If you need buffering for other endpoints, scope proxy_request_buffering off to only the upload route (location /api/upload { proxy_request_buffering off; proxy_pass http://upstream; }).
Diagnostic step 3
Alternative: enable client_body_in_file_only on and let NGINX stream to disk: client_body_in_file_only on; client_body_temp_path /var/tmp/nginx-client-body; then proxy_pass.

Continue troubleshooting

Matched from the title, description and tags