How to Increase File Upload Size in NGINX

NGINX is a powerful web server used by many high traffic websites and applications. Often users need to upload large files such as images, videos and backups, to these apps/sites. If your NGINX server is unable to handle them, then it will return ‘413 Request Entity Too Large’ error. There it is important to increase file upload size in NGINX, so that users get a smooth user experience. In this article, we will learn how to increase file upload size in NGINX.

Why Increase File Upload Size in NGINX

1. Allow Larger Uploads – These days most applications and sites need to allow users to upload large images, and videos. These files are large and so you need to increase file upload size in NGINX.

2. Improve User Experience – When file upload fails, it leads to a disappointing user experience. If you increase file upload size, then users will not get the ‘upload failed’ error message. They will be able to enjoy a better user experience.

3. Support Application Requirements – Some applications and sites like social media services and ecommerce sites, inherently need to support large file uploads. Some applications that communicate with your website, may need your server to be able to handle large file transfers. In such cases, it is better to increase file upload size.

How to Increase File Upload Size in NGINX

Here are the steps to increate file upload size in NGINX. For this purpose, we need to modify the client_max_body_size directive in NGINX server configuration file. It determines the maximum allowed file size for uploads in NGINX.

1. Open NGINX Configuration

First, we find and open NGINX configuration file in a text editor. By default, it is found at /etc/nginx/nginx.conf. If you are using virtual hosts, then open the virtual host configuration file at /etc/nginx/sites-available or /etc/nginx/conf.d/ directory.

2. Modify client_max_body_size directive

You just need to specify the max file size allowed immediately after the directive. You can add client_max_body_size directive in 3 contexts – http, server or location. Depending on where you add the directive, it will give a different result. Let us look at them one by one in detail. Here is a sample command to set the file uploads to 50Mb.

client_max_body 50M;

If you add this in http block, then it will be applied to all servers running inside that block.

http {
...
client_max_body 50M;
...
}

This is convenient if you run many servers on an NGINX instance and need to update them all at one go. If you add it to a specific server block, then it will be applied to only that server or virtual host and nothing else. Here is an example to add client_max_body directive to HTTP server.

server {
listen 80;
...
client_max_body 50M;
...
}

This gives you more control and allows you to increase allowed file size only where required. If you have added client_max_body directive to both http and server block, then the directive in server block will take precedence. So even if you have set a specific file across all servers, using http block, you can always override it using server block.

Lastly, if you need to increase file upload size only for specific URLs, then you can add this directive in location block for that location. Here is an example to increase file upload size only for /upload URL.

location /upload {
...
client_max_body 50M;
...
}

This approach allows you to precisely increase file size only for those URLs that actually need it. In case you have added client_max_body directive for both server and location blocks, then the directive in location block takes precedence.

Also, you can use suffix M for megabytes (e.g. 20M for 20 Mb), G for Gigabytes (e.g. 30G for 30Gb).

Save and quit the file when you are done.

3. Look at client_body_timeout

client_body_timeout determines the amount of time NGINX server needs to wait for a request to complete, before closing the connection with client. By default, it is set to 60 seconds.

When you increase the client_max_body directive, then you may also need to modify the value of client_body_timeout directive. Like client_max_body directive, you can add this in http, server or location blocks. In fact, most system administrators add it immediately after client_max_body directive.

client_max_body ...;
client_body_timeout ...;

4. Restart NGINX Server

Restart NGINX server to apply changes.

sudo service nginx restart
OR
sudo systemctl restart nginx

Points to Remember

Here are some key points to remember after you increase file upload size in NGINX:

1. Update upstream server – After you increase file upload size in NGINX, you need to also update the file upload parameters in upstream web server like Apache. Otherwise, even if NGINX is able to handle large file uploads, it will give an error while being transferred to upstream server. For example, if you run a PHP site, you will need to update upload_max_filesize in php.ini to match client_max_body value.

2. User intuitive error message – No matter how much you increase file size limit, there will always be instances when a user file size exceeds the limit you have set. For such cases, display an intuitive response message to improve user experience.

3. Limit file size to avoid abuse or overuse – While increasing file upload size is good, it is also important to limit it beyond a point. Otherwise, the large files will take up too much space on your server and not allow other users to upload anything. Also, large files take long time to upload. So it may also be used by hackers to bring down your site, with the help of slow file uploads.

Conclusion

In this article, we have learnt how to increase upload file size in NGINX server. As you can see, it is fairly easy and can help your site performance. We have covered different use cases in this article. We learnt how to apply it to all servers running on a single NGINX server, for all URLs on a single server, or for a single URL, or group of URLs. You can use any of the above steps, depending on your requirement.

Also read:
How to Block Image Hotlinking But Allow Google
How to Remove Trailing Slash in NGINX
How to Change NGINX Port Number in Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *