When using Nginx as a web server, you sometimes encounter an error that returns 413 Request Entity Too Large when the client tries to upload a large file. This error indicates that the entity requested by the client is larger than the size the server is willing or able to handle. This article will explain how to solve this problem by configuring Nginx.
1. Understanding 413 Request Entity Too Large Error
When the request body received by Nginx (usually the data in a POST request) exceeds its default maximum value, a 413 Request Entity Too Large error is returned. By default, Nginx limits the size of the request body to 1MB. This limitation can be adjusted by the client_max_body_size directive in the configuration file.
2. Modify Nginx configuration
2.1 Open the Nginx configuration file
First, you need to find the main configuration file for Nginx. Usually, this file is located in the /etc/nginx/ or /etc/nginx// directory. You can open it using a text editor:
sudo nano /etc/nginx/
2.2 Setting client_max_body_size
In an Nginx configuration file, you can set client_max_body_size in the http, server, or location block. Choose the right location to set according to your needs.
Global settings: If you want all server blocks and locations to use the same file size limit, you can set it in the http block:
http { client_max_body_size 10M; # The maximum allowed file size is 10MB}
Specific server block settings: If you only want to set file size limits for a specific virtual host, you can set it in the server block:
server { listen 80; server_name ; client_max_body_size 20M; # The maximum file size allowed is 20MB location / { root /var/www/html; index ; } }
Location-specific settings: If you want to set file size limits only for a specific URL path, you can set it in the location block:
server { listen 80; server_name ; location /upload { client_max_body_size 50M; # The maximum file size allowed is 50MB proxy_pass http://backend; } }
2.3 Save and exit
After the configuration is complete, save the file and exit the editor.
3. Test Nginx configuration
Before reloading Nginx, it is recommended to test the configuration file for syntax errors:
sudo nginx -t
If the test results show no problem, you can continue to reload Nginx to apply the new configuration:
sudo systemctl reload nginx
Or use the following command:
sudo service nginx reload
4. Verify the changes
To verify that the change takes effect, try uploading a file that is greater than the original limit. If the upload is successfully and no 413 Request Entity Too Large error appears, the configuration has been effective.
With the above steps, you can easily adjust the file upload size limit for Nginx, thus avoiding the 413 Request Entity Too Large error. Depending on actual needs, you can choose to set it globally, specific servers, or specific locations.
5. Method supplement
When using Nginx as the web server, sometimes it will encounter a 413 Request Entity Too Large error when the client uploads large files. This is because Nginx default client request body size limit is smaller (usually 1MB). To allow uploading larger files, you need to adjust the client_max_body_size directive in the Nginx configuration.
Here is a specific example showing how to set the maximum file size allowed to upload in an Nginx configuration file:
1. Edit Nginx configuration file
Typically, Nginx's main configuration files are located in /etc/nginx/, but more often they are modified in site-specific configuration files, which are usually located in the /etc/nginx// directory or in /etc/nginx/sites-available/ directory.
Example: Modify site configuration file
Assuming your site configuration file is /etc/nginx/sites-available/mysite, you can edit this file to add the client_max_body_size directive.
server { listen 80; server_name ; #Other configurations... location / { # Set the maximum file size allowed to upload to 10M client_max_body_size 10M; #Other location configuration... } # If you have other location blocks, you can also set them separately location /upload/ { # Set the maximum file size allowed to upload to 50M client_max_body_size 50M; #Other location configuration... } }
2. Restart Nginx service
After modifying the configuration file, you need to restart the Nginx service to make the changes take effect.
On Debian/Ubuntu systems:
sudo systemctl restart nginx
On CentOS/RHEL system:
sudo systemctl restart nginx
3. Verify the configuration
After restarting Nginx, you can verify that the configuration is successful by trying to upload a file larger than the original limit size. If the 413 error does not appear again, it means that the configuration has taken effect.
Things to note
- client_max_body_size can be set in http, server, or location context.
- Setting an excessively large value may cause excessive server resource usage, so it needs to be set reasonably according to the actual situation.
- If a reverse proxy is used, you may also need to adjust the corresponding upload limit in the backend server (such as PHP-FPM).
Through the above steps, you can easily adjust the upload file size limit of Nginx to meet the needs of different applications.
When using Nginx, if you encounter a "413 Request Entity Too Large" error, this usually means that the file the client tries to upload exceeds the maximum file size allowed by Nginx by default. The default maximum request size of Nginx is 1MB. To solve this problem, you need to modify the Nginx configuration file to increase the allowed upload file size.
Modify steps
Find the Nginx configuration file:
- The main configuration file of Nginx is usually located in a file in the /etc/nginx/ or /etc/nginx/// directory.
- If you are using Virtual Hosts, the configuration file may be located in the /etc/nginx/sites-available/ or /etc/nginx/// directory.
Edit the configuration file:
Open a configuration file using a text editor, for example using nano or vim:
sudo nano /etc/nginx/
Or for specific virtual host configuration files:
sudo nano /etc/nginx/sites-available/your-site
Add or modify the client_max_body_size directive:
The client_max_body_size directive is used to set the maximum allowable size for client requests. This value can be set to a specific number of bytes or uses units such as K, M, G.
You can add this directive to different contexts, depending on your needs:
Global settings: Set in http block, affecting all servers and locations.
http { client_max_body_size 10M; # The maximum allowed file size is 10MB ... }
Specific server settings: Set in the server block, affecting only all locations within that server block.
server { listen 80; server_name ; client_max_body_size 10M; # The maximum allowed file size is 10MB ... }
Location specific settings: Set in the location block, affecting only that location.
server { listen 80; server_name ; location /upload { client_max_body_size 10M; # The maximum allowed file size is 10MB ... } ... }
Save and exit the editor:
- If you are using nano, press Ctrl+O to save, and then press Ctrl+X to exit.
- If you are using vim, press Esc, and enter :wq to save and exit.
Test configuration file:
Before reloading Nginx, it is recommended to test whether the configuration file is correct:
sudo nginx -t
If the test passes, you will see an output similar to the following:
nginx: the configuration file /etc/nginx/ syntax is ok
nginx: configuration file /etc/nginx/ test is successful
Reload Nginx:
If the configuration file test passes, reload Nginx to apply the changes:
sudo systemctl reload nginx
Or use:
sudo service nginx reload
Verify changes
You can try uploading a file that is larger than 1MB but smaller than the size you set to confirm whether it is successfully uploaded.
If you still have problems, check Nginx's error log (usually located in /var/log/nginx/) for more information.
This is the article about the detailed explanation of how to modify the upload file size limit in Nginx 413. For more related content related to Nginx modification upload file size limit, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!