By default, the maximum file upload size in NGINX is 1MB. If a user tries to upload a file bigger than this, the Nginx throws a “413 Request Entity Too Large” error. So in this tutorial, we will learn how to increase file upload size in Nginx.
How To Increase File Upload Size in NGINX
To increase the file upload size in Nginx, we need to use the client_max_body_size
directive, which is a part of ngx_http_core_module
module in Nginx. You can set this directive either in http
, server
or location
context.
Step #1: Locate and Configure the NGINX configuration file
The Nginx configuration file is present inside the /etc/nginx/
directory. Open a terminal and edit the Nginx configuration file using your favourite text editor. In my case, I am using a vim text editor.
$ sudo vim /etc/nginx/nginx.conf
Step #2: Increase File Upload Size in NGINX
As we discussed earlier, the default file upload size in the Nginx configuration file is set to 1 MB. If you want to increase the file upload size to 20MB, you need to adjust the client_max_body_size
to 20M;
. If the client_max_body_size
directive is not present or commented out, add the same inside the http
block.
http{ ... client_max_body_size 20M; ... }
Note: For very large files, you may need to change the value of client_body_timeout
parameter. The default timeout value is 60 seconds inside the configuration file.
In case you want to increase the file upload size only for HTTPS
requests but not HTTP
requests, you need to make adjustments inside the server
block that listens at port 443
but not the server
block that listens on port 80
.
server{ ... listen 80; ... } server{ ... listen 443; ... }
The above configuration will affect all the websites or apps that are present on that particular server. If you want to adjust the configuration for any particular website or application, you need to make changes to that particular website’s Nginx configuration file inside the sites-available
directory.
Let’s say you want to make changes for example.com
website. You need to navigate inside the /etc/nginx/sites-available/
directory to edit the Nginx configuration file that you had created for example.com
.
Open the configuration file using your favourite text editor and increase the upload file size limit for a particular directive/URL. Set a location block, which affects a particular directory. In our case, it’s an upload
directory. So the configuration looks something like this:
location /uploads { ... client_max_body_size 50M; }
Step #3: Restart Nginx Web Server
Once you update the changes, run the following command:
$ sudo nginx -t
Once you run the command, Nginx will throw a configuration error if you have made any mistake inside the configuration file. If there are no errors, congratulation! You are ready to restart your Nginx web server. You need to run the following command for changes to take effect.
$ sudo service nginx restart #debian/ubuntu $ systemctl restart nginx #redhat/centos
Check out the detailed documentation for the Nginx upload module.
Conclusion
Congratulation! Your configuration was successful. I hope you will find this tutorial on how to increase file upload size in Nginx useful.
If you find any difficulty, feel free to comment below. I will try to resolve the issue as soon as possible.