0
0
NginxHow-ToBeginner · 3 min read

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_size too low can block legitimate large uploads.
  • Placing the directive outside valid blocks (like directly in the main config without http, server, or location) 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

DirectiveDescriptionExample Value
client_max_body_sizeLimits max size of client request body10m
UnitsSize units supportedk (KB), m (MB), g (GB)
ScopeWhere to set the directivehttp, server, location blocks
DefaultDefault size limit if not set1m

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.