How to Limit Request Size and Prevent Large Upload Abuse in Nginx

Without limits, an attacker (or a genuine mistake) could send extremely large requests, consuming excessive bandwidth, disk space, or memory. This guide covers configuring appropriate request size limits.

Why This Matters

An unlimited upload endpoint is a potential vector for denial-of-service abuse (filling disk space or exhausting memory with oversized requests) even without any other vulnerability being exploited — a basic, easily-overlooked hardening measure.

Setting a Global Maximum Request Body Size

http {
    client_max_body_size 10m;
}

Requests exceeding this size receive a 413 Request Entity Too Large response immediately, before consuming resources processing the full oversized request.

Setting Different Limits for Different Paths

location /api/upload {
    client_max_body_size 50m;
}

location / {
    client_max_body_size 1m;
}

An upload-specific endpoint might legitimately need a higher limit, while most of your site (form submissions, API calls) needs much less — avoid a single generous limit applied everywhere unnecessarily.

Choosing an Appropriate Limit

Set the limit based on your actual legitimate use case's realistic maximum, with modest headroom — not an arbitrarily large "just in case" value that provides little real protection.

Limiting Request Header Size

large_client_header_buffers 4 8k;

Protects against abuse via excessively large headers, a less common but still real vector.

Rate Limiting Uploads (Beyond Just Size)

Size limits alone don't prevent an abuser from sending many rapid requests at the maximum allowed size — combine with request rate limiting (see How to Rate Limit an API with Nginx) for comprehensive protection.

Testing Your Limit

curl -X POST -F "[email protected]" https://yourdomain.com/api/upload

Verify requests exceeding your configured limit correctly receive a 413 response rather than being processed or causing an error elsewhere in the stack.

Handling the 413 Error Gracefully

error_page 413 /413.html;

See How to Configure Custom Error Pages in Nginx/Apache — a friendly error message explaining the size limit is more helpful to legitimate users than a bare error response.

Considerations for Legitimate Large Uploads

If your application genuinely needs to support large file uploads (video, large documents), consider whether a direct-to-object-storage upload pattern (bypassing your application server entirely for the actual file transfer) might be more appropriate than routing large files through Nginx/your application at all — see object storage guides for this pattern.

Application-Level Validation Too

Nginx-level limits are a first line of defense — your application should still validate uploaded content (file type, actual content matching claimed type, virus scanning if appropriate) rather than relying solely on the size limit for security.

Common Errors

Legitimate uploads failing with 413 — verify the limit is actually appropriate for your real use case; check whether the limit needs to be set at multiple levels (Nginx AND your application/language runtime, which may have its own separate, potentially lower default limit).

Continue Reading

Browse more articles in Web Servers.

  • nginx client_max_body_size, limit upload size, prevent upload abuse, nginx request limits
  • 0 Usuários acharam útil
Esta resposta lhe foi útil?

Artigos Relacionados

How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource...

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...