Fix: 413 Request Entity Too Large on Nginx


This is a Fix for 413 Request Entity Too Large on nginx server and an optional PHP configuration on how to Increase Upload File Size Limit.

This problem occurs when you have fresh new server installs with nginx and PHP for me with WordPress installs lately, everyone wants their own Cloud VPS, but there is no-one to manage it or install it.

The 413 Request Entity Too Large indicates that your nginx server is not configured to accept file with the size you are trying to upload. The parameter in nginx is client_max_body_size which has the default value of 1MB.

Here are the official nginx docs on client_max_body_size if you exceed this size you will see this image

413 Request Entity Too Large

nginx configuration

You have 2 options here, you can set it globally or per website.

1. Open /etc/nginx/nginx/conf and under http {} add the following lines

vi /etc/nginx/nginx.conf
http {
    ......
    #set client body size to 20M
    client_max_body_size 20M;
    .....
}

2. Open /etc/nginx/sites-available/yourwebsite.conf and under server {} add the following lines

server {
    #set client body size to 20M
    client_max_body_size 20M;
    .....
    listen 80;
    server_name yourwebsite.com;
    .....
}

Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.

Now check nginx configuration and restart the server if everything is OK

nginx -t

You need to get

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now restart nginx server

systemctl reload nginx.service 

You are done with your nginx configuratioon


PHP configuration

Now that you have your nginx server to accept client request body of 20MB you need to increase some values in your PHP configuration file. Edit your /etc/php/7.0/fpm/php.ini and change the following values:

vi /etc/php/7.0/fpm/php.ini
; Maximum allowed size for uploaded files.
 ; http://php.net/upload-max-filesize
 upload_max_filesize = 20M

 ; Maximum size of POST data that PHP will accept.
 ; Its value may be 0 to disable the limit. It is ignored if POST data reading
 ; is disabled through enable_post_data_reading.
 ; http://php.net/post-max-size
 post_max_size = 20M

Now restart your PHP-FPM process and you are done

 systemctl restart php7.0-fpm.service 

After these modifications you should be able to upload files with MAX size of 20MB



Leave a Reply

avatar
  Subscribe  
Notify of