How to Set client_max_body_size in Nginx: Simple Guide
Set
client_max_body_size in your Nginx configuration file to limit the maximum size of client request bodies. Place it inside the http, server, or location block, then reload Nginx to apply the change.Syntax
The client_max_body_size directive sets the maximum allowed size of the client request body. It accepts a size value with optional units like k for kilobytes, m for megabytes, or g for gigabytes.
You can place this directive inside the http, server, or location blocks to control the scope of the limit.
nginx
client_max_body_size size;
Example
This example sets the maximum client request body size to 10 megabytes inside the server block. It prevents clients from sending requests larger than 10MB.
nginx
server {
listen 80;
server_name example.com;
client_max_body_size 10m;
location / {
root /var/www/html;
index index.html;
}
}Output
Nginx reloads successfully with the new client_max_body_size limit applied.
Common Pitfalls
- Forgetting to reload Nginx after changing the configuration will not apply the new limit.
- Setting
client_max_body_sizetoo low can block legitimate large uploads. - Placing the directive outside valid blocks (like directly in the main config without
http,server, orlocation) causes errors.
nginx
## Wrong placement example (causes error): client_max_body_size 5m; ## Correct placement example: http { client_max_body_size 5m; server { listen 80; server_name example.com; } }
Quick Reference
| Directive | Description | Example Value |
|---|---|---|
| client_max_body_size | Limits max size of client request body | 10m |
| Units | Size units supported | k (KB), m (MB), g (GB) |
| Scope | Where to set the directive | http, server, location blocks |
| Default | Default size limit if not set | 1m |
Key Takeaways
Set client_max_body_size inside http, server, or location blocks in nginx.conf.
Use size units like k, m, or g to specify the limit clearly.
Reload Nginx after changes to apply the new size limit.
Avoid setting the limit too low to prevent blocking valid requests.
Incorrect placement of the directive causes configuration errors.