Increasing file upload size limit in nginx

Table of Contents

A common problem while running nginx as a frond end to php based Apache+mod_fastcgi server is the size that nginx allows you to upload. Sometimes the app allows user upload images up to 2MB in size. Other times when users try to upload 1.5MB or larger in size while using nginx reverse proxy, they are getting the following error on screen: “Nginx 413 Request Entity Too Large”. The question arises: how am I going to fix this problem and allow for image upload up to 2MB in size using nginx web-server working in reverse proxy, or by stand-alone mode on Unix like operating systems?

The error that you see, “413 – Request Entity Too Large”, notates that the web server is organized to restrict large file size. Nginx is able to be set to allow the maximum size of the client request body using the client_max_body_size directive. If the size of a request surpasses the configured value, the “413 error” is returned to the client.

Thus, you need to configure both nginx and php to allow an increase in upload size.

Increase file upload size limit in PHP-Nginx

If Nginx terminates your connection when uploading large files, you will see something like below in Nginx’s error logs:

[error] 25556#0: *52 client intended to send too large body:

This error message means that you must increase PHP file-upload size limit. Following steps given below will help you troubleshoot this common problem.

  1. Changes in php.ini: to modify the max file upload size to 100MB, edit:

vim /etc/php7/fpm/php.ini

Then, set:

upload_max_filesize = 100M
post_max_size = 100M

Additional Notes:

As a rule of thumb, post_max_size must always be bigger than upload_max_filesize except for large numbers like 100M (which can safely make them equal).

Another variable is max_input_time which can potentially limit upload size. If your application allows uploads of file-size in GBs, you may need to adjust for this. If utilizing PHP-FPM behind Nginx, this setup has Nginx uploading files and then Nginx copies it to PHP. As Nginx to PHP copying will be the local operation max_input_time and may never create issue. Also, Nginx may not copy the file but may simply hand-over the location of file or descriptor records to PHP.

  1. Change in Nginx config: Add following line to http{..} block in nginx config:

http {
client_max_body_size 100m;

Note: For very large files, it may be helpful to change the value of client_body_timeout parameter. The default is 60s.

Also, be sure to reload PHP-FPM & Nginx. This can be done with the following command:

Service php7-fpm reload

service nginx reload

  1. Changes in WordPress-Multisite: When running WordPress Multisite setup, be sure to make one more change at the WordPress end.

Go to: Network Admin Dashboard >> Settings. Look for Upload Settings. Also change value for Max upload file size.

Fully Managed WordPress Hosting

Nestify’s AWS powered dedicated CPU servers keep your sites fast, secure, and always up to date.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.