How to Fix Nginx 413 Request Entity Too Large
In this tutorial, you'll learn about How to Fix Nginx 413 Request Entity Too Large. We cover key concepts, practical examples, and best practices.
The Problem
You upload a file through your web app and get 413 Request Entity Too Large or 413 Payload Too Large. The file exceeds the default Nginx client_max_body_size limit of 1 MB. This limit exists to prevent DoS attacks but blocks legitimate large uploads like profile pictures, documents, or media files.
Quick Fix
1. Increase client_max_body_size in the http block
Edit /etc/nginx/nginx.conf:
http {
client_max_body_size 50M;
...
}
2. Or set it in a specific server or location block
Edit your site config:
server {
listen 80;
server_name example.com;
# Default for this server
client_max_body_size 50M;
location /uploads {
# Override for upload endpoints
client_max_body_size 100M;
}
location /api/config {
# Stricter limit for config endpoints
client_max_body_size 10K;
}
}
3. Test the configuration and reload
sudo nginx -t
sudo systemctl reload nginx
4. Check for reverse proxy buffer issues
If Nginx is a reverse proxy to your app:
location /api/upload {
proxy_pass http://backend:3000;
client_max_body_size 100M;
proxy_request_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
}
5. Check Cloudflare limits (if applicable)
Cloudflare has its own upload limits (100 MB for paid plans, 500 MB for Enterprise). If you're behind Cloudflare, increase the limit in the Cloudflare dashboard.
6. Test the fix
# Create a test file larger than the old limit
dd if=/dev/zero of=test.bin bs=1M count=10
# Upload it
curl -X POST -F "file=@test.bin" https://example.com/upload -v
Look for a 200 or 201 response instead of 413.
7. Verify the current setting
curl -I https://example.com | grep -i "server"
Common Causes
| Cause | Context | Fix |
|---|---|---|
| Default 1 MB limit | Nginx default client_max_body_size |
Increase to match your app's max upload |
| Proxy doesn't propagate limit | Nginx reverse proxy without client_max_body_size |
Set it in the proxy location block |
| Cloudflare limit | Cloudflare has its own upload limits | Increase in Cloudflare dashboard |
| Application limit too | Both Nginx AND app must allow the size | Check both Nginx and framework configs |
Missing always flag |
CORS preflight doesn't see the header | add_header ... always |
Test Configuration Changes First
sudo nginx -t
# nginx: the configuration file syntax is ok
# nginx: configuration file test is successful
sudo systemctl reload nginx
Always run nginx -t before reloading the configuration. This validates syntax, checks file paths, and verifies that SSL certificates are accessible before applying changes.
Prevention
- Set
client_max_body_sizeto match your application's maximum allowed upload size - Set per-location limits to avoid overly permissive global settings
- Inform users about file size limits in your upload UI
- Check both Nginx and application-level upload limits when troubleshooting 413 errors
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro