Reverse Proxy & APIsupload-size.conf

How to Fix 413 Request Entity Too Large in Nginx (client_max_body_size)

By default, Nginx enforces a strict 1 Megabyte ceiling on incoming HTTP request body payloads. When users try to upload files larger than 1MB, Nginx immediately drops the connection and returns HTTP 413 Request Entity Too Large.

Interactive Nginx Config Generator

upload-size.conf
/etc/nginx/sites-available/upload-size.conf
server {
    listen 80;
    server_name example.com;

    # Increase maximum upload file size to 100MB (can be set to 0 for unlimited)
    client_max_body_size 25M;

    # Buffer client request body in memory before disk write
    client_body_buffer_size 128k;

    # Adjust upload timeout for slow client connections
    client_body_timeout 120s;

    location /api/upload {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;

        # Keep proxy timeout aligned with client upload time
        proxy_connect_timeout 120s;
        proxy_send_timeout 120s;
        proxy_read_timeout 120s;
        proxy_request_buffering off; # Stream upload directly to backend
    }
}

Directives & Architecture Explained

client_max_body_size 100M;

Configures maximum permissible client body size. Suffixes can be M for megabytes or G for gigabytes.

proxy_request_buffering off;

Disables temporary disk buffering in Nginx, streaming the upload chunk-by-chunk directly into the upstream backend.

client_body_timeout 120s;

Sets timeout interval for reading request body chunks from slow mobile connections.

Production Verification & Reload Workflow

sudo nginx -t

Verify configuration syntax

sudo systemctl reload nginx

Apply new body size limits

curl -F "file=@large_video.mp4" http://uploads.example.com/api/upload

Test uploading a file larger than 1MB to verify HTTP 200 OK

Production Troubleshooting Tips

  • β€’client_max_body_size can be placed in http {}, server {}, or specific location {} blocks depending on whether you want global or route-specific permissions.
  • β€’If using PHP, remember to also increase upload_max_filesize and post_max_size in php.ini.
  • β€’Setting client_max_body_size 0; disables body size checking entirely, but exposes the server to memory exhaustion attacks.

Frequently Asked Questions

What is the default client_max_body_size in Nginx?

The default value is 1m (1 Megabyte).

What does proxy_request_buffering off do?

It prevents Nginx from waiting until the entire 100MB file is uploaded before sending it to backend. It streams data in real-time, drastically lowering server disk IO and memory usage.