Configuring Nginx and PHP 7 stack in Linux to increase or decrease file upload size limit

PHP web applications can utilize Nginx as the reverse proxy server and PHP FPM as the upstream server. Whenever you encounter HTTP file upload issues, limitation in file upload size is one common cause.

This post shows how to adjust the file upload size limit for your application running on a Nginx-PHP stack in Linux.

Two sets of file upload size limit configuration to apply for Nginx-PHP LEMP stacks

The following digram is an illustration of how the browser communicates with the application server in a typical LEMP stack.

client to reverse proxy server to upstream server communication

As shown above, there are two machines that process HTTP requests received from HTTP clients. Therefore, we need to apply two sets of file upload size limit configuration - one for Nginx and the other for the PHP-FPM server.

Configuring file upload size limit for Nginx

Firstly, let's look at how we can configure file upload size limit for Nginx.

Locating the Nginx configuration file

A typical installation of Nginx associates the nginx binary with the PATH variable. Therefore, you can locate the Nginx configuration file by running the following command in your terminal program:

sudo nginx -t

When I run this on my Raspberry Pi 3 running Codiad Web IDE, I got the following output:

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

As can be seen, nginx reported that it is taking configurations from /etc/nginx/nginx.conf. Given that, we know where to look at for adjusting the file upload size limit for Nginx.

Configuring the file upload size limit for Nginx via client_max_body_size directive

Once you had figured out the location of the configuration file that you wish to edit, you can then proceed to add in the client_max_body_size directive with a value that you desire.

This directive defines the maximum allowed size of client request body. In addition, the default value is 1 Megabyte. When we set a value of 0, we disable the checking of the client request body size.

Since a file is included as the client request body, we will adjust the file upload size via the client_max_body_size directive.

Universal file upload size limit for HTTP requests made to all upstream servers

In case you want to allow a file smaller than 8 Megabytes to get through your Nginx server for all upstream servers, you will apply the client_max_body_size directive inside the http block:

http {
    # ...
    client_max_body_size 8m;
    # ...
}

Site specific file upload size limit

In case you want to apply another file upload size limit for HTTP request made to a specific upstream server, you need to look for the server block for that site. For example, the following configuration allows any file smaller than 1 Gigabytes to pass Nginx for my Raspberry Pi 3 file sharing website:

server {
    
    # ...     
    listen 443;
    server_name  ps.yourdomain.com;
    client_max_body_size 1024m;
    # ...

} 

Uri specific file upload size limit

You can also apply the file upload size limit to a specific Uri. For example, the following configuration will apply a file upload size limit of 2 Gigabytes for HTTP requests sent to http://www.adomain.com/upload:

server {
    
    # ...     
    listen 80;
    server_name  www.adomain.com;
    
    location /upload {
        # ...
        client_max_body_size 2048m;
        # ...
    }

    # ...

} 

Configuring file upload size limit for PHP FPM 7

Lastly, let's look at how to configure file upload size limit for PHP FPM 7.

Locating the php.ini configuration file

Before you proceed to configure the file upload size limit for PHP FPM 7, you need to find the php.ini configuration file. In general, you can run the following command to look for php.ini:

sudo find / -name php.ini

After the command completes, you may find output similar to the following in your terminal screen:

/etc/php/7.0/fpm/php.ini
/etc/php/7.0/cli/php.ini

In this case, we will edit /etc/php/7.0/fpm/php.ini to configure the file upload size limit for PHP FPM 7.

Configuring file upload size limit for PHP FPM 7 via upload_max_filesize and post_max_size

In order to change file upload size limit for PHP, we need to change two settings - upload_max_filesize and post_max_size.

The default value for upload_max_filesize is 2 Megabytes.

The default value for post_max_size is 8 Megabytes.

Note that the post_max_size has to be larger than upload_max_filesize for large files.

In addition to those two variables, you may want to set max_input_time to a higher value for large files. In this case, your clients will be given more time to upload files to your PHP FPM 7 server.

Universal file upload size limit for HTTP requests made to all PHP applications served by PHP FPM 7

To apply a common file upload size limit for all PHP applications served by PHP FPM 7, you will edit the values for upload_max_filesize and post_max_size from /etc/php/7.0/fpm/php.ini.

For example, if we want to change the file upload size limit to 200 Megabytes, we will first open up /etc/php/7.0/fpm/php.ini. After the editor loads the file, we will then change the lines with upload_max_filesize and post_max_size variables to the following:

upload_max_filesize = 200M
post_max_size = 202M

Application specific file upload size limit

To adjust the file upload size limit for different PHP applications served by PHP FPM 7, we can do so via the ini_set function.

For example, if we want to change the file upload size limit to 300 Megabytes, we will include the following PHP codes in a running script:

@ini_set( 'upload_max_size' , '300M' );
@ini_set( 'post_max_size', '302M');

Restarting Nginx and PHP FPM 7 for the new file upload size limit to take effect

Once you had included the new file upload size limit for your Nginx server and PHP FPM server, restart them:

sudo systemctl restart php7.0-fpm.service
sudo systemctl restart nginx.service

After your Nginx server and PHP FPM server had restarted, the new file upload size limit will take effect.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.