How to Limit File Upload Size in Nginx Restricting file upload sizes is crucial for preventing denial-of-service (DoS) attacks and managing server resources. This guide explains how to configure upload limits in Nginx and Dokku deployments.
Default Nginx Configuration By default, Nginx restricts client request bodies to 1MB. This includes all POST data and file uploads.
Set Upload Limit via client_max_body_size Use the client_max_body_size directive from Nginx's ngx_http_core_module to control upload sizes. This can be configured in three contexts:
- Global Configuration (Affects All Sites)
# /etc/nginx/nginx.conf
http {
...
client_max_body_size 100M;
}
- Per-Site Configuration (Server Block)
# /etc/nginx/sites-available/default
server {
...
client_max_body_size 100M;
}
- Location-Specific Configuration
location /uploads {
client_max_body_size 50M;
}
Apply Configuration Changes Test configuration syntax:
sudo nginx -t
Restart Nginx:
# Systemd systems
sudo systemctl restart nginx
Init systems
sudo service nginx restart
Handling Upload Limits Requests exceeding the limit receive a 413 (Request Entity Too Large) error
Browser display of this error may vary
Set client_max_body_size 0; to disable size checking
Special Configuration for Dokku For applications deployed via Dokku:
Create Nginx configuration directory:
mkdir -p /home/dokku/myapp/nginx.conf.d/
Create upload limit configuration:
echo 'client_max_body_size 50M;' > /home/dokku/myapp/nginx.conf.d/upload.conf
Set proper permissions:
chown dokku:dokku /home/dokku/myapp/nginx.conf.d/upload.conf
Reload Nginx:
service nginx reload
Best Practices Set limits at the most specific context needed
Always test configuration changes before applying
Monitor 413 errors in server logs
Consider application requirements when setting limits
This version features:
Clear hierarchical structure
Code blocks with syntax highlighting
Concise step-by-step instructions
Platform-specific sections (regular Nginx vs Dokku)
Important notes and best practices
Consistent command formatting