atw
atw

Reputation: 5840

Default nginx client_max_body_size

I have been getting the nginx error:

413 Request Entity Too Large

I have been able to update my client_max_body_size in the server section of my nginx.conf file to 20M and this has fixed the issue. However, what is the default nginx client_max_body_size?

Upvotes: 197

Views: 252687

Answers (8)

abisec
abisec

Reputation: 51

The default value on client_max_body_size in nginx is 1 MB. As it means that nginx will reject any request with a body size with is greater than 1MB which make "413 request entity too large" error.

Default: 1 MB & Max: 20 MB

Let's configure client_max_body_size now

nano /etc/nginx/nginx.conf

Modify now client max body size to 20 MB.

http {
    client_max_body_size 20M; 
    ...
}

Save now and restart the nginx session and service. Then try,

sudo nginx -t
sudo nginx -s reload

Additionally, I also wanna add something to secure nginx version for nginx security and prevent from CVE exploits.

sudo apt install nginx-extras
cd /etc/nginx/sites-available

more_set_headers 'Server: random_server'; # To Set a custom string as "Server"

Now time to update nginx.conf too:

       #add this on http section
       server_tokens off;
        more_clear_headers Server; 
    

As the below format:

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        server_tokens off;
        more_clear_headers Server;      

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

}

Now, test and reload nginx server:

sudo nginx -t
sudo nginx -s reload
sudo systemctl restart nginx

More details: https://devsecops.abisec.xyz/nginx/hide-nginx-server-info

Upvotes: 0

Sylvio Ruiz Neto
Sylvio Ruiz Neto

Reputation: 192

I do not recommend increase the client_max_body_size on the http or server block, you must to increase it from http -> server -> location (the target). Example:

http {
    ...
    client_max_body_size: 1m;
    ...
    server {
        ...
        client_max_body_size: 2m;
        ...
        location ~* ^/upload/(.*)$ { # the target
            ...
            client_max_body_size: 100m;
            ...
        }
    }
}

Upvotes: 0

NickB
NickB

Reputation: 5

This works for the new AWS Linux 2 environment. To fix this - you need to wrap your configuration file. You should have, if you're using Docker, a zip file (mine is called deploy.zip) that contains your Dockerrun.aws.json. If you don't - it's rather easy to modify, just zip your deploy via zip -r deploy.zip Dockerrun.aws.json

With that - you now need to add a .platform folder as follows:

APP ROOT
├── Dockerfile
├── Dockerrun.aws.json
├── .platform
│   └── nginx
│       └── conf.d
│           └── custom.conf

You can name your custom.conf whatever you want, and can have as many files as you want. Inside custom.conf, you simply need to place the following inside

client_max_body_size 50M;

Or whatever you want for your config. With that - modify your zip to now be

zip -r deploy.zip Dockerrun.aws.json .platform

And deploy. Your Nginx server will now respect the new command

More details here: https://blog.benthem.io/2022/04/05/modifying-nginx-settings-on-elasticbeanstalk-with-docker.html

Upvotes: -2

Emdadul Sawon
Emdadul Sawon

Reputation: 6077

Nginx default value for client_max_body_size is 1MB

You can update this value by three different way

1. Set in http block which affects all server blocks (virtual hosts).

http {
    ...
    client_max_body_size 100M;
}

2. Set in server block, which affects a particular site/app.

server {
    ...
    client_max_body_size 100M;
}

3. Set in location block, which affects a particular directory (uploads) under a site/app.

location /uploads {
    ...
    client_max_body_size 100M;
}

For more info click here

Upvotes: 53

Nipuna Akalana Perera
Nipuna Akalana Perera

Reputation: 181

You have to increase client_max_body_size in nginx.conf file. This is the basic step. But if your backend laravel then you have to do some changes in the php.ini file as well. It depends on your backend. Below I mentioned file location and condition name.

sudo vim /etc/nginx/nginx.conf.

After open the file adds this into HTTP section.

client_max_body_size 100M;

Upvotes: 3

ruvim
ruvim

Reputation: 8564

The default value for client_max_body_size directive is 1 MiB.

It can be set in http, server and location context — as in the most cases, this directive in a nested block takes precedence over the same directive in the ancestors blocks.

Excerpt from the ngx_http_core_module documentation:

Syntax:   client_max_body_size size;
Default:  client_max_body_size 1m;
Context:  http, server, location

Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.

Don't forget to reload configuration by nginx -s reload or service nginx reload commands prepending with sudo (if any).

Upvotes: 245

mialkin
mialkin

Reputation: 2781

Pooja Mane's answer worked for me, but I had to put the client_max_body_size variable inside of http section.

enter image description here

Upvotes: 32

Pooja Mane
Pooja Mane

Reputation: 487

You can increase body size in nginx configuration file as

sudo nano /etc/nginx/nginx.conf

client_max_body_size 100M;

Restart nginx to apply the changes.

sudo service nginx restart

Upvotes: 24

Related Questions