How to Fix NGINX 413 Request Too Large Error

Last updated on July 17th, 2024 at 05:10 am

If you try to upload a large file to NGINX server, it may give “413 : Request Entity Too Large” error. Although it is good that server blocks large uploads from overloading itself, this can also cause problems if the file upload limit is set to a small value. Nevertheless, this error should not be shown on front end, otherwise it will spoil their user experience. In this article, we will learn what 413 error code is and how to fix NGINX 413 request too large error for your website.

What is 413 : Request Too Large Error? Why do you get it?

By default, 413: Request too large error means that the size of the request sent by user to NGINX server is beyond the permissible limit set in NGINX server configuration. This occurs mainly when a user tries to upload a large file or enter a large amount of data in the site’s database. In such cases, NGINX will immediately close server connection and return response 413: Request too large. By default, NGINX supports uploads of up to 2Mb. This setting is controlled by client_max_body_size parameter. If the uploaded file exceeds this size, then you get “Request Entity Too Large” error response.

413 Request Too Large vs 414 URI Entity Too Large

Often system administrators and software developers get confused between response codes 413 and 414 returned by NGINX. This is because they display similar error messages. Error code 413 says ‘Request too Large’ whereas error code 414 says ‘URI Entity too large’. But there is a major difference between them. Status code 413 means the content of your request, that is, the body of request, is too large. Status code 414 means that the requested URL itself is too large. It has nothing to do with the content of the request body. The reasons for getting 414 URI Entity too large are completely different and need different solutions. If you are getting response code 414, then check out how to fix 414 URI Entity too large problem. It is important to remember this difference while dealing with them.

How to Fix NGINX 413 Request Too Large Error

Here are the steps to fix NGINX 413 Request Too Large Error. We will basically modify the client_max_body_size server parameter to handle a larger request size.

1. Backup NGINX Configuration File

You need to edit client_max_body_size setting in NGINX configuration file. For this purpose, take its backup before you open it.

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/backup-nginx.conf

2. Open NGINX Configuration File

Open terminal and run the following command to open NGINX configuration file in text editor.

$ sudo vi /etc/nginx/nginx.conf

3. Increase Request Size Limit

Add or modify the client_max_body_size parameter in your server configuration file. It decides the maximum body size of client request, as indicated by Content-Length request header. Below we have increased the client_max_body_size directive to 10Mb size.

client_max_body_size 10M;

You can change it as per your requirement.

4. Restart NGINX Server

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

5. Check File Permissions

Rarely but sometimes you may get this error even after you update client_max_body_size. This may be because NGINX server does not have adequate permissions to access the upload folder. If you see ‘denying uploading as opening…’ message in error.log file of NGINX, then it means there is a permission problem. Run the following command to allow NGINX to access uploads directory. Update the path below as per your requirement.

$ sudo chmown www-data:www-data /var/www/html/uploads

If you are using a separate caching server such as lighttpd, then you need to allow its user lighttpd to access this directory.

$ sudo chown lighttpd:lighttpd /var/www/html/uploads

6. PHP Configuration (Optional)

If you are using PHP with NGINX, and still getting this problem, open php.ini file to check if there is a file upload limit. Run ‘php –ini’ command on terminal/command prompt to get location of php.ini.

Please note: If you need to update the php.ini file, take its backup before you make any changes to it.

Then update the following server settings in this file.

;This sets the maximum amount of memory in bytes that a script is allowed to allocate
memory_limit = 32M

;The maximum size of an uploaded file.
upload_max_filesize = 10M

;Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize
post_max_size = 12M

Save and close the file. Until the error is fixed, it is also advisable to display custom error page in case of errors.

Conclusion

Hopefully, this article will help you fix ‘Request too Large’ Error in NGINX. If your website allows users to upload images and videos then it is advisable to set client_max_body_size directive to a large value, to avoid this error. Otherwise, your users will not be able to upload files as per their wish. However, you may want to do some kind of validation on file size to avoid malicious attackers and bots. You can use it to apply restrictions from the back end code (PHP, Python, etc.) and display intuitive user messages on web browser, instead of displaying server error page.

Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.

Also Read :
How to Remove WWW from Domain URL in NGINX
How to Redirect HTTP to HTTPS in NGINX
How to Configure Basic Authentication in NGINX
How to Create Custom 404 Page in NGINX