Nginx returns 413 Request Entity Too Large when uploading files
Problem
File uploads fail with HTTP 413 when the uploaded file exceeds Nginx's default client_max_body_size of 1MB. The error is returned by Nginx before the request even reaches the application server. This is a common issue in file upload features, avatar uploads, and document import APIs. The fix requires setting client_max_body_size in the Nginx config at the http, server, or location block level.
Error Output
413 Request Entity Too Large nginx/1.24.0
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Increase Nginx client_max_body_size for File Uploads
The error 413 Request Entity Too Large occurs because the uploaded file exceeds the default limit set by Nginx for client request bodies, which is 1MB. This limit is enforced by Nginx before the request is passed to the application server, preventing larger files from being uploaded.
Awaiting Verification
Be the first to verify this fix
- 1
Open Nginx Configuration File
Locate and open the Nginx configuration file, typically found at /etc/nginx/nginx.conf or within a specific site configuration file in /etc/nginx/sites-available/.
bashsudo nano /etc/nginx/nginx.conf - 2
Set client_max_body_size
Add or modify the client_max_body_size directive in the http, server, or location block to accommodate the expected file sizes. For example, to allow uploads of up to 10MB, add 'client_max_body_size 10M;' to the appropriate block.
nginxclient_max_body_size 10M; - 3
Test Nginx Configuration
Before applying the changes, test the Nginx configuration for syntax errors to ensure that the new settings are valid.
bashsudo nginx -t - 4
Reload Nginx
If the configuration test is successful, reload Nginx to apply the changes without downtime.
bashsudo systemctl reload nginx - 5
Verify Upload Functionality
Attempt to upload a file that exceeds the previous limit but is within the new limit to confirm that the changes have resolved the issue.
noneUse a file upload feature in your application to test.
Validation
Check the application logs and attempt to upload a file larger than 1MB but smaller than the new limit. If the upload succeeds without a 413 error, the fix has worked.
Sign in to verify this fix
Environment
- Product
- Nginx
- Environment
- production
Submitted by
Alex Chen
2450 rep