API request body size limits: align caps across layers before a 413
A request body size limit rejects oversized input before it exhausts memory, parsers, or temporary storage. Set limits at both the public ingress proxy and the application parser, and make sure the largest supported request fits every layer. An HTTP server can return 413 when a body exceeds its limit.
A request body size limit rejects oversized input before it exhausts memory, parsers, or temporary storage. Set limits at both the public ingress proxy and the application parser, and make sure the largest supported request fits every layer. An HTTP server can return 413 when a body exceeds its limit.
Why changing one number is not enough
A request may pass through a CDN or load balancer, reverse proxy, application server, and body parser. A low edge limit rejects it before it reaches the app; a high edge limit with no parser cap can let the app buffer too much data. Define limits by route and content type, then apply them at each layer.
The cap is part of the upload contract. A JSON API may need only tens of KiB, while file uploads need more. For example, imagine allowing 1 MiB for ordinary JSON and up to 25 MiB through a separate upload route. These are illustrative values, not recommended defaults or measurements. Base limits on required maximum size, processing memory, concurrent uploads, request time, and storage.
Apply the limits in order
**Define valid request sizes.** Set the real maximum for each endpoint. For large files, consider streaming or direct upload to separate object storage instead of buffering everything in application memory.
**Reject early at the public ingress.** NGINX `client_max_body_size` sets a body cap at `http`, `server`, or `location` scope; exceeding it returns 413. Its documented default is 1 MiB. Raise the cap only on routes that need it. A value of `0` disables checking, so do not use it casually on a public service.
**Set a parser limit in the application too.** A proxy setting does not protect paths that connect directly to the app. Express `body-parser` lets a JSON parser cap the parsed request using its `limit` option. Set it explicitly instead of assuming the package default is your API contract.
**Roll out and verify from a small cap.** In a test environment, send one body at the documented maximum and another one byte larger. Confirm the first is accepted and the second rejected, and that proxy-generated and app-generated 413 responses have a consistent public error shape. This is a recommended check, not a test performed here.
Example NGINX configuration (1 MiB is illustrative and should match the actual contract):
server {
location /api/ {
client_max_body_size 1m;
proxy_pass http://api_backend;
}
location /uploads/ {
client_max_body_size 25m;
proxy_pass http://upload_backend;
}
}Confirm that the intended location handles each request. If an upstream load balancer or CDN has a smaller cap, raising NGINX's value will not help: that earlier layer will reject the request.
Enforce against the received body, not only a header
When present, Content-Length can help a server reject a request early. A streaming request may not have a known length in advance, though. Do not rely on the header alone: count bytes while reading the body and stop as soon as the cap is exceeded. The protocol parser should also validate that a declared length matches the received bytes.
The transmitted body size and the size after decompression are separate resource limits. If compressed requests are accepted, set bounds for both the transmitted bytes and the expanded result. This article does not define those values or a compression-bomb defense procedure. Do not permit unbounded parsing or full-body buffering as a way around a limit.
How clients should handle 413
RFC 9110 defines 413 Content Too Large for content that exceeds what a server is willing or able to process. If the condition is temporary, the server can use Retry-After to say when a retry may work. Retrying the same body immediately will not fix a fixed size limit. The client should reduce the body or use a documented endpoint that supports chunked or multipart upload; that upload contract needs its own design.
Distinguish 413 from 415 Unsupported Media Type. An error body can safely state the allowed size and unit. Do not reveal internal routes, resource details, or server file paths. Metrics can record the layer and route that produced a 413, but logs should not retain raw bodies or sensitive data.
After deployment
Is a separate cap defined for ordinary API requests and upload routes?
Do the proxy, load balancer, and application parser limits agree with the documented contract?
Are requests limited while bytes are read, including requests with a missing or overstated `Content-Length`?
Are transmitted and decompressed sizes bounded separately?
Are public 413 responses consistent regardless of which ingress layer generated them?
Can a test environment verify the maximum and just-over-limit cases and identify which layer rejected the request?
References
[RFC 9110 §15.5.14: 413 Content Too Large](https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.14)
[NGINX ngx_http_core_module: client_max_body_size](https://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size)
[Express body-parser: request entity too large](https://expressjs.com/en/resources/middleware/body-parser.html#request-entity-too-large)