nginx 413 Request Entity Too Large on file upload
413 Request Entity Too LargeShort answer
nginx refuses any request body bigger than client_max_body_size, which defaults to 1 MB, and answers 413. Raise the value in the http, server or location block that handles the upload, test the config, and reload. If the setting seems ignored, the change was not loaded or a syntax error stopped the reload.
Fix it fast
1Find out which cause you have
Read the error log for the exact size nginx saw and the location it matched, then print the limit that is currently loaded.
sudo grep "too large body" /var/log/nginx/error.log | tail -n 3 sudo nginx -T 2>/dev/null | grep client_max_body_size2Match what you see, then jump to its fix
What does the error look like?
413 Request Entity Too Largeclient intended to send too large body: 1048576 bytes
Cause 1 of 2
The default limit of 1 MB is still in force
1Check
The default is client_max_body_size 1m. Any upload above that gets a 413 before it reaches your app.
sudo nginx -T 2>/dev/null | grep -c client_max_body_size2Fix
Set a limit that fits your largest legitimate upload. Put it in the narrowest place that works, such as the upload location. A value of 0 turns the check off, which is rarely a good idea.
server {
location /upload/ {
client_max_body_size 25m;
}
}3Verify
Send a file just under the new limit and one over it.
head -c 20000000 /dev/zero > /tmp/test20.bin
curl -s -o /dev/null -w "%{http_code}\n" -F "file=@/tmp/test20.bin" https://example.com/upload/Confirmed by nginx.org
Cause 2 of 2
The new value was never loaded
1Check
On reload nginx checks the new file and, if it is invalid, keeps running with the old configuration. A typo elsewhere in the file leaves the old 1 MB limit in place.
sudo nginx -t2Fix
Fix whatever nginx -t reports, then reload.
sudo nginx -t && sudo nginx -s reload3Verify
nginx -T now prints your value.
sudo nginx -T 2>/dev/null | grep client_max_body_sizeConfirmed by nginx.org
How do I stop it coming back?
- Set client_max_body_size per upload location, not globally, so other routes keep a small limit.
- Keep the app-level limit (framework or PHP) equal to or below the nginx one.
- Run nginx -t in the deploy script before every reload.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Uploads larger than 1 MB fail with 413 behind the Kubernetes NGINX Ingress controller
Uploads of a few MB fail, small files work
The ingress-nginx controller limits request bodies to 1m by default and answers 413 above that.
3 causes, each with a fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Your website on a Google Compute Engine VM times out on port 80
The site works with curl on the VM but not from outside
A timeout, as opposed to a refusal, usually means a firewall dropped the packets.
3 causes, each with a fix