What Causes This Error?
The Nginx 413 error occurs when a client sends a request body that exceeds the client_max_body_size directive. This commonly happens during file uploads. By default, Nginx limits request bodies to just 1 MB.
Quick Fix
Increase the client_max_body_size directive in your Nginx configuration:
# In /etc/nginx/nginx.conf (http block, applies globally)
http {
client_max_body_size 64M;
}
# Or in a specific server block
server {
client_max_body_size 64M;
}
# Or in a specific location block
location /upload {
client_max_body_size 128M;
}Apply the Change
# Test configuration syntax
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginxAlso Update PHP (If Applicable)
If your application uses PHP, you must also update PHP upload limits:
# Find your php.ini
php --ini | grep "Loaded"
# Edit php.ini
sudo nano /etc/php/8.2/fpm/php.iniSet these values to match or exceed your Nginx limit:
upload_max_filesize = 64M
post_max_size = 64MThen restart PHP-FPM:
sudo systemctl restart php8.2-fpmVerification
Test a large file upload through your application or with curl:
curl -X POST -F "file=@largefile.zip" https://yourdomain.com/uploadBest Practice
- Set limits as low as practical for your use case
- Use location-specific overrides for upload endpoints rather than increasing globally
- Monitor upload activity in your Nginx access logs