When Nginx is used as a backend server for web apps like WordPress, Drupal, etc. (which are usually PHP and MySQL Database based), many users come across the error: ‘413 – Request Entity Too Large’.
This error occurs whenever a user tries to upload a file that is greater than the configured maximum upload file size. The default limit for file upload size is 1 MB as defined in the Nginx configuration.
[ You might also like: How to Fix 504 Gateway Timeout in Nginx Server ]
Usually, this error is encountered by a WordPress admin trying to install a plugin for the first time. For example, I tried to install the ‘Download Manager’ WordPress plugin on my local WP setup, it gives me the same error:
Change Upload File Size Limit in Nginx
Open the Nginx configuration file in either Vim or any text editor of your choice.
$ sudo vim /etc/nginx/nginx.conf
Add the directive ‘client_max_body_size‘. This directive defines the maximum size of the body of a client request. By default, the value of this directive is 1MB. Increasing this value will enable you to upload a file larger in size which is sent with the request.
Add the following line under the ‘http’ block:
client_max_body_size 16M;
You can even put a higher number instead of 16 MB if required. Now, save and close the file.
Restart Nginx with the following command.
$ sudo systemctl restart nginx
Now your Nginx setup will allow you to upload a file up to 16 MB in size. Let’s try installing the plugin again.
Even after configuring the Nginx file size, the PHP file upload limit is still blocking the upload. Let’s see how to change it.
Change PHP Upload Size Limit
Nginx is used in conjunction with PHP-based web apps, and the issue shown above can arise due to a limit in PHP file upload size.
Open the file ‘php.ini‘ which is present in the PHP configuration directory.
$ sudo vim /etc/php/7.4/fpm/php.ini OR $ sudo vim /etc/php.ini
Go to the variables upload_file_maxsize, i.e. the maximum size of a file that is allowed to be uploaded, and post_maxsize, i.e. the maximum size of a post request.
As a file is uploaded as a part of the post request, post_maxsize should be always greater than upload_file_maxsize.
Change the values as required.
upload_max_filesize = 16M post_max_size = 20M
Save and exit the file.
Restart PHP, as well as Nginx, for the new configuration to take place:
$ sudo systemctl restart php7.4-fpm $ sudo systemctl restart nginx
You should replace php7.4 in the above command with whatever is your PHP version, or even just ‘php’ if that is the PHP service name in your machine.
The newly specified file size is allowed now for file upload in the web app, and the plugin proceeds with the installation successfully.
Conclusion
We have seen how to deal with the issue of a request entity being too large in an Nginx-based web app. Note that not only PHP but if other CGIs are used with Nginx the issue can occur; in which case users can increase the limit in the configuration file for the particular CGI.
Thanks for reading and let us know your thoughts in the comments below!