Default Nginx Client_max_body_size

Better Stack Team
Updated on October 7, 2024

The default value for the client_max_body_size directive in Nginx is 1 megabyte (1M). This setting controls the maximum size of the client request body, which includes file uploads and POST data.

If a client tries to upload a file or send data that exceeds this limit, Nginx will return a 413 Request Entity Too Large error.

How to Check or Modify the Default Value

To check or modify this value, follow these steps:

1. Check Current Configuration

You can check the current client_max_body_size value by looking at your Nginx configuration files. If it’s not explicitly set, it will default to 1MB.

Find the Configuration File:

Common locations for the Nginx configuration file are:

  • /etc/nginx/nginx.conf
  • /etc/nginx/conf.d/
  • /etc/nginx/sites-available/

Command to Find Configuration:

 
grep client_max_body_size /etc/nginx/nginx.conf /etc/nginx/conf.d/* /etc/nginx/sites-available/*

Explanation:

  • This command searches for the client_max_body_size directive across common Nginx configuration files.

2. Modify client_max_body_size

To change the maximum allowed body size, edit the Nginx configuration file and set the client_max_body_size directive to your desired value.

Edit Configuration File:

Open the Nginx configuration file in a text editor:

 
sudo nano /etc/nginx/nginx.conf

Add or Modify client_max_body_size:

Add or update the directive within the http block or in a specific server block.

Example Configuration:

 
http {
    # Other configurations...

    client_max_body_size 10M;  # Increase limit to 10MB

    # Other configurations...
}

Or within a specific server block:

 
server {
    listen 80;
    server_name your-domain.com;

    root /var/www/html;
    index index.html;

    client_max_body_size 10M;  # Increase limit to 10MB

    location / {
        try_files $uri $uri/ =404;
    }
}

3. Test the Configuration

After making changes, test the configuration to ensure there are no syntax errors:

 
sudo nginx -t

Explanation:

  • This command checks the syntax of the Nginx configuration files.

4. Reload or Restart Nginx

Apply the changes by reloading or restarting Nginx:

Reload Command:

 
sudo systemctl reload nginx

Or restart (if necessary):

 
sudo systemctl restart nginx

Summary

  • Default Value: The default client_max_body_size in Nginx is 1M (1 megabyte).
  • Modify Value: Edit the nginx.conf file or appropriate configuration file and set the client_max_body_size directive to your desired limit.
  • Test and Apply: Use nginx -t to test the configuration and systemctl reload nginx to apply the changes.

Adjusting this directive allows you to control the maximum size of client request bodies and can help prevent issues with file uploads and large POST requests.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github