ERROR

How to Fix Nginx 413 Request Entity Too Large

Quick Fix Summary

TL;DR

Increase `client_max_body_size` in your Nginx configuration file and reload the service.

Nginx returns a 413 error when an HTTP request body exceeds the configured `client_max_body_size` limit. This is a server-side configuration issue, not a client problem.

Diagnosis & Causes

  • Default `client_max_body_size` is too small (1MB).
  • Uploading large files via web forms or API.
  • Misconfigured location block overriding server block.
  • Missing `client_max_body_size` directive in proxy_pass location.
  • Configuration in wrong context (e.g., http vs server).
  • Recovery Steps

    1

    Step 1: Locate and Edit Your Nginx Configuration

    Identify the main Nginx config file and the specific server or location block handling the request.

    bash
    # Find main config file
    nginx -t 2>&1 | grep 'nginx.conf'
    # Common config paths: /etc/nginx/nginx.conf, /etc/nginx/sites-available/
    2

    Step 2: Increase `client_max_body_size` in the HTTP/Server Context

    Set a global or per-server limit. This applies to all location blocks unless overridden.

    nginx
    # In http or server block (e.g., /etc/nginx/nginx.conf or site config)
    http {
        # Global limit (applies to all servers)
        client_max_body_size 100M;
    }
    server {
        # Server-specific limit (overrides global)
        client_max_body_size 250M;
        ...
    }
    3

    Step 3: Increase `client_max_body_size` in a Specific Location Block

    For precise control, set the limit only for upload endpoints or proxy paths.

    nginx
    server {
        ...
        location /upload {
            client_max_body_size 500M;
            # Other directives...
        }
        location /api/ {
            client_max_body_size 50M;
            # Other directives...
        }
        # CRITICAL for reverse proxies
        location / {
            proxy_pass http://backend;
            client_max_body_size 100M; # Must be set here too
            proxy_request_buffering off; # For streaming large bodies
        }
    }
    4

    Step 4: Test Configuration and Reload Nginx

    Validate syntax and apply changes without dropping connections.

    bash
    # Test configuration syntax
    sudo nginx -t
    # If test passes, reload Nginx
    sudo systemctl reload nginx  # Or: sudo nginx -s reload
    5

    Step 5: Verify the Fix and Check Headers

    Confirm the new limit is active and check for conflicting client-side limits.

    bash
    # Check active config for the directive
    sudo nginx -T 2>/dev/null | grep -A2 -B2 'client_max_body_size'
    # Use curl to test (adjust URL and file)
    curl -X POST -H "Content-Type: application/json" -d @large_file.json http://your-server/upload
    6

    Step 6: (Advanced) Adjust Buffer Timeouts for Large Uploads

    Prevent timeouts during slow uploads by increasing buffer-related timeouts.

    nginx
    http || server || location {
        client_max_body_size 1G;
        client_body_timeout 300s;    # Max time to read client body
        client_header_timeout 300s;  # Max time to read client header
        proxy_read_timeout 300s;     # For proxied requests
        keepalive_timeout 300s;
    }

    Architect's Pro Tip

    "When using Nginx as a reverse proxy, you must set `client_max_body_size` in BOTH the location block that receives the request AND ensure your backend app (e.g., Node.js, PHP-FPM) has a compatible limit."

    Frequently Asked Questions

    Does `client_max_body_size` affect GET requests?

    No. It only applies to requests with a body, typically POST, PUT, and PATCH methods. GET requests are unaffected.

    I increased the limit but still get 413. Why?

    Check nested location blocks overriding your setting, ensure you reloaded Nginx, and verify your backend server or upstream proxy (e.g., Cloudflare) doesn't have a lower limit.

    What's the maximum value for `client_max_body_size`?

    You can set it to 0 to disable checking, but practical limits are system memory and disk space. For huge files (10G+), consider direct client-to-object storage uploads.

    Should I set `proxy_request_buffering off`?

    Use this for streaming very large files to the backend to reduce disk I/O on the Nginx server, but ensure your backend can handle streamed requests.

    Related Nginx Guides