Uploads a file and hits the following Nginx error page.
Table of Contents
1. Reason
The 413 Request Entity Too Large error in Nginx means the request body size exceeds the limit defined by the client_max_body_size.
In simple words, the uploaded file size exceeds the limit.
2. Fix the 413 Request Entity Too Large
We can adjust the client_max_body_size to limit the file upload size in Nginx.
2.1 Find the Nginx configuration file
Find the Nginx configuration file, typically at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf or sites directory (depending on setup).
2.2 Adjust the client_max_body_size
Open the Nginx configuration file with the editor and adjust the client_max_body_size directive. Add or update the client_max_body_size inside the http, server or location block depending on where we want to apply the limit.
The below code snippets set the upload limit to 5M at the server block level.
server {
listen 80;
server_name mkyong.com;
client_max_body_size 5M;
...
}
The below code snippets set the upload limit to 10M at the location block level.
server {
listen 80;
server_name mkyong.com;
location /upload/ {
client_max_body_size 10M;
...
}
location / {
...
}
}
2.3 Reload Nginx
After we made the changes, save the Nginx configuration file and reload the Nginx to apply the changes:
Using systemd:
sudo systemctl reload nginx
Or using the direct nginx command:
sudo nginx -s reload
3. client_max_body_size default limit
In Nginx, the default value for client_max_body_size is 1m.
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location
Refer to the official doc – client_max_body_size.
Note
Increasing the limit of client_max_body_size for larger uploads will expose the risk of potential denial-of-service attacks.