Ugacomp

How to configure Nginx to handle large file uploads

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

In this article, we will explore the necessary configurations and provide command examples to help you set up Nginx for handling large file uploads effectively.

Adjusting Client-Side Configuration

Before delving into Nginx configurations, it’s important to ensure that the client-side is configured to allow large file uploads. Adjust the client_max_body_size directive in the Nginx configuration to match the maximum size of files you want to accept. This directive sets the maximum allowed size of the client request body.

http {
    client_max_body_size 100M;
}

In this example, the client_max_body_size is set to 100 megabytes. Adjust this value based on your application’s requirements.

Configuring Nginx for Large File Uploads

Open the Nginx main configuration file, typically located at /etc/nginx/nginx.conf, and add or modify the http block.

http {
    # Other configurations...

    client_max_body_size 100M;

    # Additional configurations for large file uploads
    sendfile off;
    client_body_buffer_size 1M;
    client_header_buffer_size 1k;
    proxy_buffer_size 1k;
    proxy_buffers 4 1k;
    proxy_busy_buffers_size 1k;
}

The above example includes configurations such as turning off sendfile to ensure proper handling of large files, adjusting buffer sizes to accommodate large uploads.

Configuring the Server Block

Navigate to your Nginx server block configuration, often found in /etc/nginx/sites-available/default, and add or modify the necessary directives.

server {
    # Other server configurations...

    location /upload {
        client_max_body_size 100M;

        # Additional configurations for large file uploads
        proxy_pass http://backend_server;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }
}

Here, we’ve specified the maximum client body size within the location block to override the global setting if needed. Additionally, we set up proxy headers for forwarding information to the backend server.

PHP Configuration File

In addition to Nginx configurations, it’s crucial to ensure that your PHP settings are tuned to accommodate large file uploads. Let’s explore the necessary adjustments in PHP configurations within the context of an Nginx server.

Locate your PHP configuration file, commonly named php.ini. This file is often found in /etc/php/{PHP_VERSION}/fpm/php.ini for PHP-FPM installations. Open the file and modify the following settings:

; php.ini

upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 600

Adjust the upload_max_filesize and post_max_size values to match the maximum file size you want to allow. Set max_execution_time to a value that provides sufficient time for large file uploads to complete.

Restart PHP-FPM

Once you have identified the PHP-FPM service name, use the following command to restart the service:

sudo service php-fpm restart

Replace php-fpm with the actual service name if it’s different in your case.

After restarting PHP-FPM, you can check the status to ensure that it restarted successfully without any errors:

sudo service php-fpm status

Nginx PHP Location Block

In your Nginx server block or a dedicated configuration file, ensure that the PHP requests are appropriately configured to handle large file uploads.

server {
    # Other server configurations...

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php{PHP_VERSION}-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "upload_max_filesize=100M \n post_max_size=100M \n max_execution_time=600";
        include fastcgi_params;
    }

    location /upload {
        client_max_body_size 100M;

        # Additional configurations for large file uploads
        proxy_pass http://backend_server;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }
}

Ensure that the PHP_VALUE parameter includes the same values for upload_max_filesize, post_max_size, and max_execution_time as set in your php.ini file. This ensures consistency in handling large file uploads between Nginx and PHP configurations.

Conclusion

By configuring both Nginx and PHP settings appropriately, you can create a robust environment capable of handling large file uploads in your web application. Remember to tailor the values to match your specific requirements and server capabilities. Regularly monitor and adjust these configurations as needed to optimize performance and ensure a seamless user experience.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.