How to Increase File Upload Size on Nginx

Endrit Qerreti

Endrit Qerreti

When you install Nginx, the directive client_max_body_size  specifies the upload size that you want to allow to be uploaded on your site/server.

By default the limit is set to 1 MB so if you try to upload something bigger than that you'll get the error " 413 (Request Entity Too Large)", to increase this limit or to remove it completely follow the steps below.

Step 1 - Open Nginx Config

Type the command below to edit nginx conf file

sudo nano /etc/nginx/nginx.conf

Note: you can always choose any text editor you want, to do this simply replace "nano" with your favorite text editor

Step 2 - Find  client_max_body_size Directive

Once you have opened the file nginx.conf on the terminal, look for the directive client_max_body_size, depending on your website/configuration this directive can be located under http, server or location.

http block - To increase the limit for all server blocks

http {
    ...
    client_max_body_size 50M;
}

location block - To Increase the limit only for specific directories on your site

location /my-uploads {
    ...
    client_max_body_size 50M;
}

server block - To increase the limit for specific server/website

server {
    ...
    client_max_body_size 50M;
}

Then simply change the limit, the directive should look like this

If you want to increase it to 50mb then simply replace 1m to 50m, then press ctrl+x to save changes you made then press Y to confirm it.

Note: The value 0 will disable the limit completely.

Step 3 - Reload & Restart Nginx

This is final step, all you need to do now is to check nginx syntax and then proceed by reloading and restarting nginx.

1) Check Nginx Syntax

Type the command below to test nginx configuration file, if no errors come up then you should be good to go to next command

sudo nginx -t 

2)Reload Nginx

Type the command below to reload nginx configuration file, this command is useful when you don't want to restart nginx to apply the new changes you made.

sudo nginx -s reload 

3) Restart Nginx

Type command below in order to restart nginx, restarting might be necessary sometimes however you should run it only after you have checked the nginx syntax, if nginx file doesn't have the correct syntax restarting nginx will prevent nginx from starting.

In a real life scenario your server/website will be down because restarting will stop and fail to start nginx again,  since syntax wasn't correct nginx is unable to start.

sudo systemctl restart nginx or 

sudo /etc/init.d/nginx reload

Conclusion

In this tutorial you learned how to increase client_max_body_size limit on Nginx.