How To Increase File Upload Size In Nginx?

Published July 21, 2024

Problem: Nginx File Upload Size Limitations

Nginx, a web server, has default file upload size limits that can stop large file transfers. This limit can cause problems when uploading big documents, media files, or data sets through web applications on Nginx servers.

Locating the Nginx Configuration File

Common Nginx Configuration File Locations

The Nginx configuration file is usually in one of these locations:

  • /etc/nginx/nginx.conf
  • /usr/local/nginx/conf/nginx.conf

These are the standard paths for the main Nginx configuration file. The exact location may differ based on how Nginx was installed and your operating system.

Tip: Check Package Manager

If you installed Nginx using a package manager like apt or yum, you can often find the configuration file location by checking the package information. For example, on Ubuntu or Debian systems, you can use:

dpkg -L nginx | grep nginx.conf

This command lists all files installed by the Nginx package and filters for the nginx.conf file.

Troubleshooting Missing Configuration Files

If you can't find the Nginx configuration file in the expected places, try these steps:

  1. Check Nginx installation: Run nginx -v to verify if Nginx is installed and see its version.

  2. Use command-line tools to search for config files:

    • Search for nginx.conf:
      sudo find / -name nginx.conf
    • Look for any file with "nginx" in its name:
      sudo find / -name "*nginx*"

If these steps don't help you find the configuration file, Nginx might not be installed on your system or it could be in a non-standard location.

Modifying Nginx Settings to Increase Upload Size

Editing the Nginx Configuration File

To increase the file upload size limit in Nginx, you need to edit the Nginx configuration file:

  1. Open the configuration file with a text editor:

    sudo nano /etc/nginx/nginx.conf

    Use the correct path if your configuration file is in a different location.

  2. Find the section to make changes. You can place the client_max_body_size directive in the http, server, or location context. It's often in the server block.

Adjusting the client_max_body_size Parameter

To change the maximum allowed size of the client request body:

  1. Add or modify the client_max_body_size directive:

    client_max_body_size 100M;

    This sets the maximum upload size to 100 megabytes. Change this value as needed.

  2. If the directive exists, change the value. If not, add the line to the right section of your configuration file.

  3. Use M for megabytes or G for gigabytes. Don't use MB or GB as these won't work.

  4. You can set different limits for different locations:

    location /upload {
       client_max_body_size 500M;
    }

    This sets a 500MB limit for the /upload location.

  5. Save the changes and exit the text editor.

Test your configuration and reload Nginx after making these changes. Make sure your PHP settings (if applicable) match your new Nginx upload size limit.

Tip: Check PHP upload settings

If you're using PHP with Nginx, remember to also adjust PHP's upload settings. Edit your php.ini file and modify these values:

upload_max_filesize = 100M
post_max_size = 100M

Replace 100M with your desired limit. Restart PHP-FPM after making these changes.

Testing and Applying the New Configuration

Verifying Nginx Configuration Syntax

After changing the Nginx configuration file, check for syntax errors before applying the changes. Use this command to verify the configuration:

sudo nginx -t

This command tests the Nginx configuration files and reports errors. If the test passes, you'll see a message stating the configuration test is successful and the syntax is ok.

If there are errors, the command will show details about the issue and its location in the configuration file. Fix these errors before continuing.

Tip: Backup Your Configuration

Before making changes to your Nginx configuration, create a backup of your current configuration file. This allows you to quickly revert changes if needed:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

Reloading or Restarting Nginx

Once you've confirmed the configuration syntax is correct, apply the new settings. There are two main methods:

  1. Reload Nginx: This method doesn't interrupt active connections. Use this command:

    sudo nginx -s reload

    Or, if you're using systemd:

    sudo systemctl reload nginx
  2. Restart Nginx: If a reload doesn't apply all changes, you may need to restart Nginx. This will interrupt all active connections:

    sudo systemctl restart nginx

After reloading or restarting Nginx, the new file upload size limit should be active. Test your file upload function to confirm that larger files can now be uploaded.

Monitor your server's performance after increasing the upload size limit, as handling larger file uploads may need more server resources.