How to Solve Nginx Error 413 - Request Entity Too Large

Share:
how-to-solve-nginx-error-413-request-entity-too-large

Sometimes when developing web application, we need to allow users to upload some files. It could be avatar image, introductory video, etc. The first issue we'll encounter with default Nginx and PHP installation is related with upload file size. To encounter this issue, we need to adjust the maximum value parameter for both Nginx and PHP.

Nginx Configuration

Nginx can be adjusted to allow the maximum size of the client request body using client_max_body_size directive. If the size of a request exceeds the configured value, the 413 (Request Entity Too Large) error returned to the client. You will see an error like this:

Open the Terminal or login to the remote server using ssh client. Type the following command to edit your nginx.conf using a text editor such as vi and must be run as root:

# vi /usr/local/nginx/conf/nginx.conf

Add the following line to http or server or location context to increase the size limit in nginx.conf, enter:

client_max_body_size 2M;

The client_max_body_size directive assigns the maximum accepted body size of client request, indicated by the line Content-Length in the header of request. If size is greater the given one, then the client gets the error "Request Entity Too Large" (413).
Save and close the file. Reload the nginx webserver, enter:

# service nginx reload

PHP configuration

Your php installation also put limits on upload file size. Edit php.ini and set the following directives:

;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 = 2M
;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 = 3M

If you are using PHP-FPM, restart it as follows:

$ sudo systemctl restart php-fpm
## OR ##
$ sudo systemctl restart php7.0-fpm.service
## OR ##
$ sudo /usr/local/etc/rc.d/php-fpm restart

Save and close the file. Make sure you reload/restart back-end apache or nginx web server as per your setup. See "PHP Increase Upload File Size Limit" tutorial for more information.

Final Words

Well, that's it. I hope that you now know how to solve Nginx error 413 - Request Entity Too Large. If you run into any issues or have any feedback feel free to drop a comment below.

 

Tags
Share:

0 comment

Leave a reply

Your email address will not be published. Required fields are marked *