Home / PHP upload_max_filesize and nginx

PHP upload_max_filesize and nginx

I’ve recently set up a new server with PHP & Nginx using packages from dotdeb. The default upload_max_filesize and post_max_size for this setup with php-fpm is 2M. To adjust it, the Nginx config also needs a configuration change, otherwise you’re still stuck with a smaller limit.

PHP config

On my install the PHP config file is at /etc/php5/fpm/php.ini but it may be somewhere else for you, depending on the distro and packages used.

Find and edit the post_max_size and upload_max_filesize options to suit, noting that post_max_size should be equal to or greater than upload_max_filesize. In my case, I set them both to 10MB like so:

post_max_size = 10M
upload_max_filesize = 10M

You’ll then need to restart/reload the PHP process that’s running. In my case this is php-fpm on Debian so you’d do it like this:

sudo service php5-fpm

Nginx config

By default, Nginx only allows 1MB to be sent as part of the request, so event the PHP default of 2MB is too big (never mind the 10MB I’ve just upped it to) and it will fail to allow this to upload. You’ll see something like this in the Nginx error log:

2014/02/11 13:44:10 [error] 7523#0: *1750090 client intended to send too large body: 2075258 bytes, client: 222.153.57.185, server: www.example.com, request: "POST /admin/files/upload/8458 HTTP/1.1", host: "www.example.com", referrer: "http://www.example.com/admin/files/browse/8458"

Edit the main nginx.conf file (/etc/nginx/nginx.conf on the Debian install) and add this to the http { } section, setting the amount to the same (or greater) than that set in the PHP config:

client_max_body_size 10m;

Now reload Nginx so the new setting takes effect, on a Debian install, like this:

sudo service nginx reload

Note that you don’t have to set it to 10MB as I have in this post; set it to an approriate value for you.