413 Request Entity Too Large - File Upload Issue

Better Stack Team
Updated on October 7, 2024

The 413 Request Entity Too Large error occurs when the server is unable to process a request because the payload (such as a file upload) exceeds the allowed size limit. In the context of Nginx, this typically happens when the file being uploaded is larger than the maximum allowed size configured in Nginx.

To resolve this issue, you need to adjust the client_max_body_size directive in your Nginx configuration. Here’s how you can do that:

1. Modify the Nginx Configuration

You need to increase the client_max_body_size setting in your Nginx configuration to allow larger file uploads.

Edit the Nginx Configuration File:

  • Global Setting (applies to all server blocks): Edit the main Nginx configuration file, usually located at /etc/nginx/nginx.conf.

     
    http {
        client_max_body_size 100M;  # Increase the size limit to 100MB
        # Other settings...
    }
    
    
  • Server Block Setting (applies to a specific server block): Edit the configuration file for the specific server block where you want to allow larger uploads, usually found in /etc/nginx/sites-available/ or /etc/nginx/conf.d/.

     
    server {
        listen 80;
        server_name example.com;
        root /var/www/html;
    
        client_max_body_size 100M;  # Increase the size limit to 100MB
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        # Other settings...
    }
    
    

Explanation:

  • client_max_body_size: Sets the maximum size of the client request body, including file uploads. Increase this value according to your needs.

2. Restart Nginx

After making changes to the Nginx configuration, you need to restart Nginx to apply the changes.

Restart Command:

 
sudo systemctl restart nginx

Alternative Command (if using init.d):

 
sudo service nginx restart

3. Adjust Application-Level Settings

If your application or server software (e.g., PHP-FPM, Node.js) also imposes a size limit, you need to adjust those settings as well:

For PHP-FPM:

Edit the PHP configuration file, typically located at /etc/php/7.x/fpm/php.ini (replace 7.x with your PHP version).

Settings to Modify:

 
upload_max_filesize = 100M
post_max_size = 100M

Restart PHP-FPM:

 
sudo systemctl restart php7.x-fpm

For Node.js:

If using a Node.js server, you may need to configure the maximum body size in your application code.

Example Using 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

4. Verify Changes

To ensure that the new settings are in effect:

  • Check Nginx Configuration Syntax:

     
    sudo nginx -t
    
  • Test File Upload:

    Try uploading a file that was previously causing the 413 error to confirm that the issue is resolved.

5. Additional Considerations

  • Check for Proxy Server Limits: If you are using a proxy server or load balancer (e.g., an upstream proxy like HAProxy or a CDN), ensure that its configuration also supports larger file uploads.
  • Monitor Server Resources: Large file uploads can impact server performance. Monitor server resources and adjust configurations as necessary to handle the increased load.

Summary

To resolve the 413 Request Entity Too Large error in Nginx:

  1. Modify Nginx Configuration: Increase client_max_body_size in the Nginx configuration file.
  2. Restart Nginx: Apply changes by restarting the Nginx service.
  3. Adjust Application-Level Settings: Ensure that application servers (e.g., PHP-FPM, Node.js) are configured to handle large file uploads.
  4. Verify Changes: Test file uploads and check configuration syntax.
  5. Consider Additional Factors: Check for proxy server limits and monitor server resources.

By following these steps, you can address and prevent the 413 Request Entity Too Large error, allowing your server to handle larger file uploads effectively.

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