/config/nginx/client-max-body-size
nginx client_max_body_size: fix 413 Request Entity Too Large
- tool
- nginx
- file
- nginx.conf (http, server or location context)
- key
- client_max_body_size
- type
- size (1m, 50m, 1g; 0 disables the check)
- default
- 1m
client_max_body_size is the largest request body nginx accepts, 1 MB by default. A bigger upload is rejected with 413 Request Entity Too Large before it reaches the backend, and the error log says client intended to send too large body. Raise it where uploads happen: client_max_body_size 50m; in the server or location block.
Reproduce and fix
A Node backend counts the bytes it receives; /api/ uses the default limit and /upload/ sets 5 MB:
mkdir -p logs temp
cat > backend.js <<'EOF'
require("http").createServer((req, res) => {
let n = 0;
req.on("data", (c) => (n += c.length));
req.on("end", () => res.end(`backend got ${n} bytes\n`));
}).listen(4211, "127.0.0.1");
setTimeout(() => process.exit(0), 4000);
EOF
cat > nginx.conf <<'EOF'
pid logs/nginx.pid;
events {}
http {
access_log off;
server {
listen 127.0.0.1:4212;
location /api/ { proxy_pass http://127.0.0.1:4211; }
location /upload/ { client_max_body_size 5m; proxy_pass http://127.0.0.1:4211; }
}
}
EOF
node backend.js &
nginx -p . -c nginx.conf -e logs/error.log &
sleep 1
head -c 2000000 /dev/zero > 2mb.bin
curl -s --data-binary @2mb.bin http://127.0.0.1:4212/api/ | grep '<title>'
curl -s --data-binary @2mb.bin http://127.0.0.1:4212/upload/
grep -o 'client intended to send too large body: [0-9]* bytes' logs/error.log
nginx -p . -c nginx.conf -e logs/error.log -s stopOutput
<head><title>413 Request Entity Too Large</title></head> backend got 2000000 bytes client intended to send too large body: 2000000 bytes
Where to set it
http {
client_max_body_size 10m; # default for every server
server {
server_name files.example.com;
location /upload/ {
client_max_body_size 200m; # only for the upload endpoint
}
}
}The most specific block wins: a location value overrides the server value, which overrides http. client_max_body_size 0; disables the check.
Values
| Value | Effect |
|---|---|
| 1m (default) | bodies up to 1 MB; enough for forms and JSON, too small for most file uploads |
| 20m, 100m | typical values for image, document or backup uploads |
| 1g | gigabyte suffix (g or G), accepted by this directive (shown below) |
| 0 | no limit at all; only use it when the backend enforces its own limit |
| 10MB, 1.5m | rejected by nginx -t with "client_max_body_size" directive invalid value |
Only the suffixes k/K, m/M and g/G exist, and the number must be an integer. A value copied from another tool's syntax fails the configuration test:
mkdir -p logs temp
check() {
printf 'pid logs/nginx.pid;\nevents {}\nhttp {\n client_max_body_size %s;\n server { listen 127.0.0.1:5027; }\n}\n' "$1" > nginx.conf
echo "--- client_max_body_size $1"
nginx -p . -c nginx.conf -e stderr -t 2>&1 | sed -E 's/^[0-9/]+ [0-9:]+ \[([a-z]+)\] [0-9#]+: /[\1] /'
}
check 1g
check 10MBOutput
--- client_max_body_size 1g nginx: the configuration file ./nginx.conf syntax is ok nginx: configuration file ./nginx.conf test is successful --- client_max_body_size 10MB [emerg] "client_max_body_size" directive invalid value in ./nginx.conf:4 nginx: configuration file ./nginx.conf test failed
Pitfalls
- The 413 page may not show in the browser. The nginx docs warn that browsers cannot correctly display this error, so users may report a failed or reset upload rather than a 413. Check the error log for
client intended to send too large body. - Raising it in the wrong block. A
client_max_body_sizeinhttpis overridden by a smaller value in theserverorlocationthat actually handles the upload.nginx -T | grep client_max_body_sizelists every value in the merged configuration. - Forgetting the reload. The new limit applies only after
nginx -t && nginx -s reload(see nginx -t). - Large bodies go to disk. Bodies bigger than
client_body_buffer_size(8k or 16k by default, two memory pages) are written to a temporary file inclient_body_temp_path, so large upload limits need free disk space there. - Slow uploads are a different error. If the client stops sending for longer than
client_body_timeout(60s by default, measured between two reads), nginx returns 408, not 413.
FAQ
Where do I put client_max_body_size: http, server or location?
Any of the three. Put a modest default in http and a larger value only in the location that receives uploads, so other endpoints keep the small limit.
I raised client_max_body_size and still get 413. Why?
Either nginx was not reloaded, a more specific block sets a smaller value, or the 413 comes from another layer such as the application, a load balancer or an ingress controller. The nginx error log line client intended to send too large body tells you whether nginx rejected it.
Does the limit apply to GET requests?
It applies to the request body of any method. GET requests normally have no body, so in practice it affects POST, PUT and PATCH uploads.