Fix Nginx "client intended to send too large body" Error
Quick Answer
- Conclusion: The error occurs when Nginx's default
client_max_body_size(1 MB) is smaller than the HTTP request body your client is sending, typically during file uploads. - First checks: Verify the actual file size being uploaded, check which Nginx context (http, server, or location) needs the change, and confirm you run
nginx -s reloadafter editing. - Minimal fix: Add
client_max_body_size 100m;inside thehttp,server, orlocationblock of your nginx.conf, then reload Nginx. - Environment: Applies to any Nginx version (no module required); works with any backend (Django, Flask, Node.js, PHP, etc.). The backend may also have its own size limits that need adjustment.
What Problem It Solves
Nginx defaults to a 1 MB limit on the client request body. When a user uploads a file larger than that, Nginx rejects the request with a 413 Request Entity Too Large error and logs client intended to send too large body. This fix raises or removes that limit so legitimate large uploads succeed.
Root Cause Analysis
The error is purely a configuration boundary. Nginx's client_max_body_size directive defines the maximum allowed size of the client request body. If the Content-Length header exceeds this value, Nginx immediately returns 413 without forwarding the request to the backend.
Common reasons the fix appears not to work:
- The directive is placed in the wrong context (e.g., inside a
locationblock that doesn't match the upload URL). - The configuration file was edited but Nginx was not reloaded (
nginx -s reload). - The backend (e.g., PHP-FPM's
upload_max_filesize, Gunicorn'slimit_request_line, or uWSGI'sbuffer-size) has its own lower limit.
Minimal Working Configuration
Add the directive in the appropriate block of your Nginx configuration. The most common approach is to set it globally in the http block:
NGINXhttp { client_max_body_size 100m; # ... other settings }
To apply it only to a specific server:
NGINXserver { listen 80; server_name example.com; client_max_body_size 50m; # ... }
To apply it only to a specific upload endpoint:
NGINXserver { # ... location /upload { client_max_body_size 200m; # proxy_pass or other handlers } location /api { client_max_body_size 10m; } }
After editing, reload Nginx:
BASHnginx -s reload
Common Errors and Fixes
| Error | Likely Cause | Fix |
|---|---|---|
client intended to send too large body | client_max_body_size too low or missing | Add or increase the directive in the correct context |
413 Request Entity Too Large | Same as above, or backend limit | Check both Nginx and backend limits |
client_max_body_size has no effect | Wrong context or no reload | Place directive in http, server, or matching location block; run nginx -s reload |
upstream sent too big header while reading response header from upstream | Proxy buffer too small for large responses | Increase proxy_buffer_size and proxy_buffers (e.g., proxy_buffer_size 128k; proxy_buffers 4 256k;) |
Production Notes and Security Checks
- Reload required: Configuration changes take effect only after
nginx -s reloador a full restart. - Backend synchronization: If you proxy to a backend (uWSGI, Gunicorn, PHP-FPM), that backend may also limit request body size. Adjust those settings in parallel.
- Memory and disk impact: A high
client_max_body_sizecombined with request buffering can consume significant memory or temporary disk space. Consider settingproxy_request_buffering off;to reduce memory pressure for large uploads. - Security: Restrict upload file types and scan for malware. Use a dedicated temporary directory with restricted permissions. Monitor access logs for 413 errors to detect abuse attempts.
- HTTPS considerations: If using SSL, ensure
proxy_buffer_sizeis large enough to handle large headers.
FAQ
Q: I set client_max_body_size but still get 413. What else could be wrong?
A: Possible causes: (1) The directive is in a context that doesn't apply to the request (e.g., inside a location block that doesn't match). (2) You didn't reload Nginx after editing. (3) Your backend (PHP-FPM, Gunicorn, uWSGI, etc.) has its own request size limit that is lower. (4) A CDN or load balancer in front of Nginx has its own limit.
Q: What value should I use for client_max_body_size? Are there performance risks?
A: Choose a value slightly above your largest legitimate upload (common range: 10 MB to 200 MB). Setting it too high increases memory usage (if buffering is on) and disk I/O (if buffered to temp files). Mitigate by using proxy_request_buffering off; and setting reasonable timeouts (proxy_read_timeout, proxy_connect_timeout). Monitor system resources after deployment.
Q: How do I set different size limits for different upload paths?
A: Place separate client_max_body_size directives inside individual location blocks. Each location inherits the parent setting but can override it. For example:
NGINXlocation /upload { client_max_body_size 200m; } location /api { client_max_body_size 10m; }