Recently I have configured an Nginx webserver and after few days, some users started getting the following error.
Error uploading file: 413 Request Entity Too Large nginx.
This issue is because of the file upload size limit in nginx. It is always safe to keep the least possible file upload limit in Nginx. The default value is 1 MB. So if we are not setting this explicitly to a larger value, we will get this error for any uploads larger than 1 MB.
To increase this limit, we need to set the following properly in the Nginx configuration file. The following property will set the body size to 100 MB. You can configure any value to this property.
client_max_body_size 100M;
We can set this property in three different ways.
Adjust the payload size globally in Nginx
To increase the size globally across all the virtual hosts in Nginx (Server blocks), add the property in the http section of the Nginx configuration file
http {
...
client_max_body_size 200M;
}
Adjust the payload size for a specific endpoint in Nginx
To adjust the request payload size for a specific endpoint, we need to add the property within the location section. An example is given below
location /uploads {
...
client_max_body_size 200M;
}
Adjust the payload size for a specific virtual host (server)
To adjust the request payload size for a specific virtual host (server block). Add the property within the server block. An example is given below.
server {
...
client_max_body_size 200M;
}
Note: It is recommended to increase the size only to the least minimal endpoints. Enabling it globally is not safe.
After updating the configuration, validate the changes and reload the nginx.
nginx -t service nginx reload
I hope this tip is useful. Feel free to comment if you have any questions. Subscribe the blog to get updates on the upcoming articles.
thanks!