How to Increase Max File Upload Size in Nginx


how to increase file upload size in nginx

By default, Nginx has a file upload size limit of 1MB. But sometimes your users may need to be able to upload larger files to your website. In this case, they will see an error message unless you increase the file upload limit in Nginx. Here’s how you can increase max file upload size in Nginx.

 

How to Increase Max File Upload Size in Nginx

To set the maximum file upload size in Nginx, we use the client_max_body_size directive. It is a part of Nginx’s ngx_http_code_module. Depending on its value, Nginx will check the size of your request body. If it exceeds the value specified in this directive, Nginx will show an error message.

It sets the maximum size of request body, by setting the Content-length request header.

Depending on where you place this directive in your server config file, Nginx will behave differently. You can set client_max_body_size in http, location and server context. Let’s look at how to increase max file upload size in Nginx, for each case

 

HTTP Block

If you place the client_max_body_size directive in http block, it will set the max file upload size for all server blocks (virtual hosts)


http {
...
client_max_body_size 100M;
}

 

Server Block

If you place the client_max_body_size directive in server block, it will set the max file upload size only for that particular website/app.


server {
...
client_max_body_size 100M;
}

 

Location Block

If you place the client_max_body_size directive in location block, it will set the max file upload size only for that particular directory.


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

 

In each of the above case, if the request size exceeds the specified value, the client will see a 413 (Request Entity Too Large) error.

 

Save and close the file. Restart Nginx to apply changes


$ systemctl restart nginx #systemd
$ service nginx restart #sysvinit

 

Disabling File Size Check

If you don’t want Nginx to check the uploaded file size at all, then just set the client_max_body_size directive to 0.

This will disable checking of client request body size. However, it is not advisable to do it. Attackers can exploit it to launch Denial-of-Service (DOS) attacks by uploading really long files to your web server.

 

That’s it! Now you know how to increase max file upload size in Nginx.

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!