0
0
Nginxdevops~5 mins

Request size limits (client_max_body_size) in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes users upload files or send data that is too large for a web server to handle safely. Nginx can limit the size of client requests to prevent overload or abuse by setting a maximum allowed size.
When you want to stop users from uploading files larger than 10MB to your website.
When your server has limited memory and you want to avoid crashes from huge requests.
When you want to protect your backend services from receiving unexpectedly large data.
When you want to return a clear error message if a user sends too much data.
When you want to control bandwidth usage by limiting request sizes.
Config File - nginx.conf
nginx.conf
events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        server_name example.com;

        client_max_body_size 10M;

        location /upload {
            root /var/www/html;
            index index.html;
        }
    }
}

The client_max_body_size 10M; line sets the maximum allowed size of client request bodies to 10 megabytes. If a client sends more data, nginx will reject it with an error.

The server block defines the website listening on port 80 for example.com. The location /upload block serves files from /var/www/html.

Commands
This command tests the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command tries to upload a file larger than 10MB to test if nginx blocks it according to the limit.
Terminal
curl -X POST -F 'file=@largefile.zip' http://example.com/upload
Expected OutputExpected
curl: (22) The requested URL returned error: 413 Request Entity Too Large
-X POST - Specifies the POST method to send data.
-F - Uploads a file as form data.
Key Concept

If you remember nothing else from this pattern, remember: client_max_body_size controls the largest request size nginx will accept from clients.

Common Mistakes
Setting client_max_body_size inside the wrong block like 'events' instead of 'http' or 'server'.
Nginx ignores the directive if placed in an invalid context, so the limit won't apply.
Always place client_max_body_size inside the 'http', 'server', or 'location' blocks.
Not reloading nginx after changing the configuration.
Changes won't take effect until nginx reloads or restarts.
Run 'sudo nginx -t' to test, then 'sudo systemctl reload nginx' to apply changes.
Setting client_max_body_size too low without considering legitimate upload sizes.
Users may get blocked unexpectedly, causing frustration.
Choose a size that balances protection and user needs, like 10M for typical uploads.
Summary
Add 'client_max_body_size' in nginx.conf inside the server block to limit request size.
Test the configuration with 'sudo nginx -t' before reloading nginx.
Reload nginx with 'sudo systemctl reload nginx' to apply the new limit.
Use curl or similar tools to verify that large requests are blocked with a 413 error.