Web Development

NGINX returns 413 Request Entity Too Large for chunked uploads only when client uses Transfer-Encoding: chunked

theocode
1mo ago
Environment

Environment

Nginx 1.18 on Ubuntu 20.04 proxying to a Node.js 14 upload endpoint; client uploads use Transfer-Encoding: chunked (no Content-Length). client devices include mobile browsers and curl.

Symptoms

Symptoms

Large uploads (>5MB) occasionally receive 413 errors when sent with chunked encoding. Same clients succeed when sending with explicit Content-Length (standard multipart/form-data). NGINX error.log shows 'client intended to send too large body' only for chunked requests.

Observations

Observations

client_body_max_size in nginx.conf set to 8m. For chunked requests, NGINX first buffers body to client_body_temp_path; disk space is sufficient. Increasing client_body_buffer_size didn't help. Upstream logs show no request arriving for the failing chunked attempts. curl -T with --upload-file (sets Content-Length) succeeds consistently.

Already attempted

Already attempted

Increased client_body_max_size to 100m, cleared temp path, reloaded nginx — still saw 413 for some chunked uploads. Tried disabling buffering (proxy_request_buffering off) but failures persisted for certain clients.

Question

Question

Why does NGINX reject chunked uploads with 413 even when client_body_max_size is larger than the upload, and how to accept chunked uploads reliably while maintaining buffering protections?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
erikethernet Answer
user
Posted 1mo ago • 0 confirmations

NGINX treats chunked bodies differently and can hit buffering limits before Content-Length checks. First check client_body_temp_path permissions and inspect temp files for failing uploads. As a quick test, set proxy_request_buffering off on the upload location so the upstream gets the chunked stream directly; if uploads succeed, you know NGINX buffering is the issue. Then tune client_body_max_size and client_body_buffer_size or keep buffering off and harden upstream to handle backpressure. Verify by reproducing the chunked upload with curl and watching nginx logs; roll back buffering changes if upstream resource issues appear.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

NGINX client_body_max_size or buffering thresholds reached earlier in chunked path due to how chunked body is buffered to temp files 80%
Intermittent corrupt or malformed chunked streams from specific clients causing NGINX to consider them malformed and reject 25%
Permission or disk I/O error on client_body_temp_path causing NGINX to treat buffering as failed and respond 413 20%

Summary

NGINX uses a different code path for requests without Content-Length (chunked) and will apply client_body_max_size per read/temporary buffer limits and may reject early if it estimates body exceeds configured limits or temp file policies hit. Some client implementations send initial chunks with large leading headers or try to send 'infinite' streams. Proxy_request_buffering off can bypass buffering but may expose upstream to backpressure. Mismatch between client expectations and NGINX buffering parameters or temporary-file permissions can cause early 413s.

Suggested Fixes

Diagnostic step 1
Enable detailed request logging for failed requests (log $request_body_file) and inspect client_body_temp_path for temp files and permissions.
Diagnostic step 2
Temporarily set proxy_request_buffering off for the upload location to let upstream handle chunked streams, then load-test to ensure upstream handles backpressure.
Diagnostic step 3
Keep client_body_max_size sufficiently high and increase client_body_buffer_size so initial buffer can absorb larger first chunks.

Continue troubleshooting

Matched from the title, description and tags