Nginx

What is client_max_body_size in Nginx

For web servers, Nginx is a freely available, open-source Linux application. It also serves as a reverse proxy, an email proxy server (SMTP, POP3, IMAP), and a load balancer for TCP, HTTP, and UDP servers. The Nginx architecture comprises a master process and its worker processes. The master is responsible for reading the configuration file and managing the worker processes, while the workers are responsible for actual request processing.

What is the default file upload size in Nginx

File upload size in Nginx is limited to 1MB by default, which is the lowest limit for some platforms, particularly for sites that permit their users to upload items such as videos and images. In such a case, when a user tries to upload files of 1.5MB or greater than the default value, he will encounter the error: “Nginx 413 Request Entity Too Large”. This “Nginx 413 Request Entity Too Large” error indicates that the webserver is set up to limit large file sizes. Now how will you resolve this issue? In Nginx, we can fix this error by setting the value of the “client_max_body_size” file.

What is client_max_body_size in Nginx

The client_max_body_size variable, which is also part of the nginx_http_core_module, can change the file upload size in Nginx. You can set the client_max_body_size in the block of server, HTTP, or location of the Nginx configuration file.

Context of client_max_body_size: location, HTTP, server

Default value of client_max_body_size: 1M

Here is the Syntax of client_max_body_size:

If you set the client_max_body_size to “0“, the client request body size is not checked. In the other cases, you can limit the file upload size according to your requirement.

Now, we will demonstrate how to set the client_max_body_size in Nginx. So let’s start!

How to set client_max_body_size in Nginx

First of all, open up your terminal by pressing “CTRL+ALT+T“. Next, to check what is the current value of “client_max_body_size” in Nginx, we will execute the below-given command for opening the Nginx configuration file in your nano editor:

$ sudo nano /etc/nginx/nginx.conf

The Nginx configuration file will have the following settings:

How to increase file upload size across the entire website using client_max_body_size in Nginx

In the opened “/etc/nginx/nginx.conf” file, look for the line assigning the value to the “client_max_body_size” variable in the “http {}” section. You can add the “client_max_body_size” value manually if you cannot find it in the configuration file.

For instance, we will increase the size limit of the file limit in Nginx to “200M“. These settings will permit us to upload any file across the entire website up to

How to increase file upload size for a specific server using client_max_body_size in Nginx

Nginx also offers you the facility to increase the file upload size for a specific service. You can set the file upload values by using the “client_max_body_size” variable. For instance, we want to set the “client_max_body_size” to 200M for the server that listens at port 80. To do so, we will add the following line to the specific server {} block in our Nginx configuration file:

How to increase file upload size for a specific directive using client_max_body_size in Nginx

Want to increase the file upload size of a specific directive? In the Nginx configuration file, you can also add this functionality using the “client_max_body_size” variable.

Now, for the “uploads” folder, we will add the below-given code in the location{} block of the Nginx configuration file to increase its “client_max_body_size” value to the 200M:

In the Nginx configuration file, after settings your “client_max_body_size” value, press “CTRL+O” to save the changes we have made:

Now, restart the Nginx service, and you are all ready to go!

$ sudo systemctl restart nginx

Conclusion

Nginx is a freely available web server that works as a load balancer, email proxy, and reverse proxy. The default file upload size in Nginx is 1M, and if a user tries to upload a file greater than the default value, the site will encounter the “Nginx 413 Request Entity Too Large” error. In the Nginx configuration file, you can set the “client_max_body_size” value for increasing the file upload size. This post explained “client_max_body_size” and how you can increase the file upload size for any directive, server, or entire website using “client_max_body_size”.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.