413 request entity too large error solution

I am personally using Nginx as my primary web server with PHP-FPM but also having some setups with Nginx as a proxy to Apache Server. Many times, when I try to upload a file or made a large HTTP request, Nginx gives the error 413 request entity too large. That’s why I thought this issue needs to be documented.

413 request entity too large

Error 413 request entity too large occurs when a client request exceeds the maximum accepted body size by the webserver configuration. The two most used webservers are Apache and Nginx.

The default request size allowed by Apache is unlimited but in Nginx, it is only 1 MB. That’s why when we try to upload a file larger than 1 MB, Nginx giving the request entity too large error. In this article, you will find how to control HTTP request size in Apache and Nginx.

Fixing request entity too large error in Nginx

In Nginx, the maximum client request body size is defined by the client_max_body_size directive. By default, it is defined in the Nginx configuration file located in /etc/nginx/nginx.conf. However, you can directly define it into either your http, location or server block.

Open the configuration file in any text editor (vim, vi, or nano).

sudo vim /etc/nginx/nginx.conf
#############OR##############
sudo nano /etc/nginx/nginx.conf

In the below example, the maximum body size is 20 MB. However, if you do not want to set a limit for request body size then you can set its value to 0 for unlimited.

server {
    ...
    client_max_body_size 20M;
    ...
}

After that, you have to reload the Nginx service for applying changes. Use the below command for reloading the Nginx service.

sudo service nginx reload
###### OR #######
sudo systemctl reload nginx.service

Apache Configuration

In the Apache web server, the max client request body size is defined by the LimitRequestBody directive. By default, it is set to 0 which means unlimited. But you can set it to your desired value. You can define the LimitRequestBody directive in your http.cong file or you can directly define it in your .htaccess file.

In the below example, we limit the max request body size to 20 MB. We have to define the value in bytes.

LimitRequestBody 20971520

After making changes, you have to reload the Apache server for applying changes. Use the below command to reload the Apache Service.

service apache2 reload

PHP Configuration

If you are using PHP, then you also remember to set the maximum upload size in the PHP configuration. You can set it by defining the below two directives in your php.ini file.

#The maximum size of an uploaded file.
upload_max_filesize = 20M

#max size of post data.this value must be larger than upload_max_filesize
post_max_size = 30M

After that, If you are using Apache then reload Apache service by the below command.

service apache2 reload

And if you are using PHP-FPM, then you have to restart PHP-FPM service by using the below command.

Note: Change php7.4-fpm to your current php-fpm version.

sudo systemctl restart php7.4-fpm.service
###################OR#####################
sudo service php-fpm restart

Do not forget to share your thoughts in the comment section.

Leave a Comment