How to Fix “413 Request Entity Too Large” Error in Nginx Despite Setting client_max_body_size Properly


2 views

Many developers encounter the frustrating situation where they've properly configured client_max_body_size in Nginx, yet still receive the "413 Request Entity Too Large" error, especially when handling massive file uploads (like the 26GB case mentioned).

First, let's verify all the necessary settings for large file uploads:

http {
    # These should be in http, server, or location context
    client_max_body_size 30000M;
    client_body_buffer_size 200000k;
    client_body_temp_path /path/to/tmp_nginx;
    client_body_in_file_only clean;

    server {
        location /upload {
            # Override if needed
            client_max_body_size 30000M;
            proxy_pass http://backend;
        }
    }
}

Several factors could cause this error to persist:

# 1. Multiple location blocks conflict
location /api {
    client_max_body_size 10M; # This might override your specific location
}

# 2. Missing proxy settings
location /upload {
    proxy_request_buffering off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

# 3. Incorrect temp path permissions
chown -R nginx:nginx /media/ss/synology_office/server_Seq-Cap/tmp_nginx
chmod -R 770 /media/ss/synology_office/server_Seq-Cap/tmp_nginx

When basic configuration isn't solving the problem:

# Check effective Nginx configuration
nginx -T

# Verify the actual limit being applied
grep -r "client_max_body_size" /etc/nginx/

# Enable debug logging
error_log /var/log/nginx/error.log debug;

Here's a complete working configuration for large file uploads:

server {
    listen 443 ssl;
    server_name example.com;

    # Global upload settings
    client_max_body_size 30000M;
    client_body_buffer_size 500M;
    client_body_temp_path /mnt/nginx_temp 1 2;
    client_body_in_file_only clean;
    client_body_timeout 300s;
    client_header_timeout 300s;
    keepalive_timeout 300s;
    send_timeout 300s;
    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;
    proxy_send_timeout 300s;

    location /supercap/pipe {
        # Explicitly set (though inherited)
        client_max_body_size 30000M;
        
        # Critical proxy settings
        proxy_request_buffering off;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;

        proxy_pass http://api/supercap/pipe;
    }

    # Temp directory setup
    location /media/ss/synology_office/server_Seq-Cap/tmp_nginx {
        internal;
        alias /media/ss/synology_office/server_Seq-Cap/tmp_nginx;
    }
}

After implementing these changes:

# Test configuration
nginx -t

# Reload Nginx
systemctl reload nginx

# Verify with curl test
curl -X POST -H "Content-Type: application/octet-stream" \
--data-binary @largefile.bin \
https://example.com/supercap/pipe

When uploading a massive 26GB file to an Nginx server, you might encounter the frustrating 413 Request Entity Too Large error even after configuring client_max_body_size to a sufficiently large value (30,000M in this case). Here's the location block configuration:

location /supercap/pipe {
  client_max_body_size 30000M;
  client_body_buffer_size 200000k;
  proxy_pass http://api/supercap/pipe;
  client_body_temp_path /media/ss/synology_office/server_Seq-Cap/tmp_nginx;
}

While client_max_body_size is correctly set, several other factors could trigger the 413 error:

  • The setting might be overridden in a higher-level configuration
  • Filesystem permissions on the temporary directory
  • Upstream server limitations
  • Network-level constraints

Here's a complete solution that addresses all potential bottlenecks:

# Main nginx.conf or in your server block
http {
  # Global setting as fallback
  client_max_body_size 30000M;
  
  # Buffer and temp file settings
  client_body_buffer_size 10M;
  client_body_in_file_only off;
  client_body_temp_path /var/nginx/client_body_temp 1 2;
  
  # Timeout settings for large uploads
  client_header_timeout 300s;
  client_body_timeout 300s;
  keepalive_timeout 300s;
  send_timeout 300s;
  proxy_read_timeout 300s;
  proxy_connect_timeout 300s;
  proxy_send_timeout 300s;
}

server {
  # Server-specific override
  client_max_body_size 30000M;

  location /supercap/pipe {
    # Explicitly set again for this location
    client_max_body_size 30000M;
    client_body_buffer_size 200M;
    
    # Temp path with proper permissions
    client_body_temp_path /media/ss/synology_office/server_Seq-Cap/tmp_nginx;
    
    # Proxy settings
    proxy_pass http://api/supercap/pipe;
    proxy_request_buffering off;
    proxy_buffering off;
  }
}
  1. Check configuration hierarchy: Use nginx -T to see all active configurations
  2. Verify temporary directory:
    sudo mkdir -p /media/ss/synology_office/server_Seq-Cap/tmp_nginx
    sudo chown -R www-data:www-data /media/ss/synology_office/server_Seq-Cap/tmp_nginx
    sudo chmod -R 770 /media/ss/synology_office/server_Seq-Cap/tmp_nginx
    
  3. Test with curl:
    curl -v -X POST -H "Content-Type: application/octet-stream" \
    --data-binary @largefile.bin http://yourserver/supercap/pipe
    

If the issue persists after these changes, consider:

# Check nginx error logs
tail -f /var/log/nginx/error.log

# Verify system limits
ulimit -a
df -h # Check disk space

# Monitor during upload
watch -n 1 "ls -lh /media/ss/synology_office/server_Seq-Cap/tmp_nginx"