How to Edit Nginx.conf to Increase File Size Upload

Better Stack Team
Updated on October 7, 2024

To increase the maximum file size upload in Nginx, you'll need to modify the nginx.conf file to adjust the client_max_body_size directive. This directive controls the maximum size of the client request body, including file uploads.

Here’s a step-by-step guide on how to edit nginx.conf to increase the file size upload limit:

1. Locate the nginx.conf File

First, find the nginx.conf file that your Nginx instance is using. Here’s how:

Check with Nginx Command:

 
nginx -t

Output Example:

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

This output shows the path to your configuration file.

2. Edit the nginx.conf File

Open the nginx.conf file using a text editor with appropriate permissions.

Common Locations for nginx.conf:

  • Debian/Ubuntu/CentOS/Fedora: /etc/nginx/nginx.conf
  • Alternate Locations: /usr/local/nginx/conf/nginx.conf, /usr/local/etc/nginx/nginx.conf

Command to Edit:

 
sudo nano /etc/nginx/nginx.conf

Or use another text editor:

 
sudo vim /etc/nginx/nginx.conf

3. Increase client_max_body_size

Locate the http block or server block where you want to set the maximum file size. Add or update the client_max_body_size directive to increase the allowed size.

Example Configuration:

 
http {
    # Other configurations...

    client_max_body_size 100M;  # Increase the size limit to 100MB

    # Other configurations...
}

Explanation:

  • client_max_body_size: This directive sets the maximum allowed size of the client request body, including file uploads. The size can be specified in megabytes (M) or gigabytes (G).

4. Restart Nginx

After making changes to the nginx.conf file, restart Nginx to apply the new configuration.

Restart Command:

 
sudo systemctl restart nginx

Alternative Command (if using init.d):

 
sudo service nginx restart

5. Verify Changes

To confirm that the changes have been applied:

  1. Check Nginx Error Logs: Ensure there are no errors related to the configuration.

    Command:

     
    sudo tail -f /var/log/nginx/error.log
    
  2. Test File Upload: Attempt to upload a file larger than the previous limit to verify that the new limit is effective.

If you're using a web application that also has file upload size limits, make sure to adjust those settings as well:

  • For PHP (if using PHP-FPM): Update upload_max_filesize and post_max_size in php.ini.
  • For Node.js: Configure body size limits in your application code.

Example for PHP (in php.ini):

 
upload_max_filesize = 100M
post_max_size = 100M

Example for Node.js (Express.js):

 
const express = require('express');
const app = express();

app.use(express.json({ limit: '100mb' }));
app.use(express.urlencoded({ limit: '100mb', extended: true }));

// Your routes and other settings

Summary

To increase file size upload limits in Nginx:

  1. Locate nginx.conf: Use nginx -t or check common locations.
  2. Edit nginx.conf: Add or update client_max_body_size in the http block or specific server block.
  3. Restart Nginx: Apply changes by restarting the service.
  4. Verify Changes: Check logs and test file uploads.
  5. Adjust Related Settings: Ensure application-level settings are consistent with the new limit.

By following these steps, you can effectively increase the file size upload limit for your Nginx server.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github