413 Request Entity Too Large Error – An Easy Solution

Today I was trying different themes on one of my WordPress website. I downloaded many themes on my computer before. After I chose my theme file and hit the Install Now button to upload it to my server, Nginx said: 413 Request Entity Too Large. Most of the time, you only have to edit Nginx configuration file to allow large file upload.

Increasing Upload File Size For All Server Blocks

If you have multiple websites (server blocks) on a single Nginx server and you want to increase the upload file size for all of them. Then you need to edit nginx.conf file

sudo vi /etc/nginx/nginx.conf

Put the following text into the http section

client_max_body_size 2M;

The above Nginx directive means the maximum file size for uploading is 2 megabytes. The default value is 1M. So if you don’t specify it and upload a file that is larger than 1 megabytes, then your will get a 413 request entity too large error. You can change this value to your liking.

Save and close the file. Then reload Nginx configuration. You don’t need to restart Nginx.

sudo systemctl reload nginx

or

sudo /etc/init.d/nginx reload

Increasing Upload File Size For a Single Server Block

If you want to increase the upload file size for a single server block, then you need to edit the server block file. On Debian/Ubuntu server:

sudo vi /etc/nginx/sites-available/yourdoman.com

On CentOS/Redhat Server:

sudo vi /etc/nginx/conf.d/yourdomain.com.conf

And add the following line to the server section. Again, you can change the value to your needs.

client_max_body_size 2M;

Now reload your Nginx configuration.

Uploading Files That is Larger Than 2 Megabytes

Please note that PHP also sets a limit of upload file size. The default maximum file size for uploading is 2M. So when you upload a file larger than 2M, you will get this error

The uploaded file exceeds the upload_max_filesize directive in php.ini.

then you need to change the value in PHP.

Edit the php.ini file. On Debian/Ubuntu:

sudo vi /etc/php5/fpm/php.ini

On CentOS/Redhat:

sudo vi /etc/php.ini

Find the following line:

upload_max_filesize = 2M

Change the value to the same value of your Nginx upload file size. Then reload php-fpm.

sudo service php-fpm reload or sudo systemctl reload php-fpm

Your system may have php5-fpm or php7.0-fpm instead of php-fpm.

Checking if it’s already set

Before set the client_max_body_size value in Nginx, it’s wise to check if the value is already set but you don’t know it. Change your working directory to /etc/nginx/ and execute the following command:

sudo grep -R 'client_max_body_size'

The above command will find the directive in all files under your Nginx config directory. You can also directly open a server block file sudo vi /etc/nginx/conf.d/filename.conf to see if that server block has already set the value.

Overriding

If you specify a value of client_max_body_size both in nginx.conf file and in your server block file, then the value in the server block file overrides the value set in nginx.conf file.