Nginx - Client_max_body_size Has No Effect

Better Stack Team
Updated on October 7, 2024

If the client_max_body_size directive in Nginx has no effect, it usually means that either the directive is not correctly placed in the configuration or that other configurations are overriding it. Here’s a comprehensive checklist to diagnose and fix the issue:

1. Verify Configuration Placement

The client_max_body_size directive can be placed in several contexts:

  • http block: Applies globally to all server blocks.
  • server block: Applies to a specific server.
  • location block: Applies to a specific location within a server.

Make sure the directive is placed in the appropriate context based on your needs.

Example Configuration

 
http {
    client_max_body_size 10M;  # Global setting

    server {
        listen 80;
        server_name example.com;

        client_max_body_size 20M;  # Server-specific setting

        location /upload {
            client_max_body_size 30M;  # Location-specific setting
        }
    }
}

2. Ensure Proper Syntax

Verify that the directive is correctly spelled and properly formatted in your configuration file.

  • Correct Syntax: client_max_body_size 10M;
  • Incorrect Syntax: client_max_body_size: 10M; (with colon)

3. Check for Configuration Overrides

Make sure that there are no conflicting client_max_body_size directives in different contexts that might override your setting.

Steps:

  1. Search for All Instances

    Check for all occurrences of client_max_body_size in your configuration files:

     
    sudo grep -r "client_max_body_size" /etc/nginx/
    
  2. Review and Resolve Conflicts

    If you find multiple instances, ensure that the directive is set to the desired value in the most specific context where it should apply. The directive in the most specific context (e.g., location) will override those in more general contexts (e.g., server).

4. Ensure No Syntax Errors

A syntax error in the configuration file can cause Nginx to revert to default settings. Verify the syntax of your configuration:

 
sudo nginx -t

Resolve any syntax errors before reloading Nginx.

5. Reload or Restart Nginx

After making changes, ensure you reload or restart Nginx to apply the new configuration:

  • Reload Nginx:

     
    sudo systemctl reload nginx
    
  • Restart Nginx:

     
    sudo systemctl restart nginx
    

6. Check for Other Limits

Verify that there are no other limits imposed by upstream servers (e.g., PHP-FPM or other backend services) that might affect the maximum body size.

  • PHP-FPM Configuration: Check the upload_max_filesize and post_max_size directives in the PHP configuration file (php.ini).

     
    upload_max_filesize = 10M
    post_max_size = 10M
    
  • Proxy Limits: If you’re using a proxy, check if there are limits set in the proxy configuration.

7. Examine Nginx Logs

Check the Nginx error logs for any warnings or errors related to the request size.

  • View Nginx Error Log:

     
    sudo tail -f /var/log/nginx/error.log
    

Look for any messages related to request size or body size.

Summary

  1. Correct Context: Place client_max_body_size in the appropriate context (http, server, or location).
  2. Proper Syntax: Ensure correct syntax and spelling.
  3. Resolve Conflicts: Check for and resolve any conflicting directives.
  4. Verify Syntax: Test the configuration for errors.
  5. Reload Nginx: Apply changes by reloading or restarting Nginx.
  6. Check Other Limits: Ensure that other related configurations (e.g., PHP settings) are consistent with the desired body size limit.
  7. Review Logs: Check Nginx logs for any related issues.

By following these steps, you should be able to resolve issues with client_max_body_size and ensure that Nginx correctly applies the desired body size limit.

Got an article suggestion? Let us know
Explore more
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