Fixing nginx 413 Request Entity Too Large

The Problem and the solution

Set up a new website, domain and all, nginx is up and running. Users try to use your website and all of a sudden, your users see a 413 Request Entity Too Large error!

By default, nginx has this at 1 megabyte, which if you're running anything that involved uploading any files, especially videos or high res pictures, it just won't do.

There are several ways to go about fixing this and several values this directive will accept:

Values

600K = 600 kilobytes
50M = 50 megabytes
1G = 1 gigabyte

Location

  • http level (applies to all)
    /etc/nginx/nginx.conf

    http {
    client_max_body_size 50M;
    }
  • server level (applies to a single server/domain/subdomain/app)
    /etc/nginx/sites-available/*.conf

    server {
    client_max_body_size 100M;
    }
  • location level (applies to a particular location that matches a rule)
    /etc/nginx/sites-available/*.conf inside a server block.

    location {
    client_max_body_size 150M;
    }

Solution

The best way to use this is to be as specific as possible, which means the location level.
If you have a specific route that ingests uploads from users, that would be the best one to put it as.

For any other usage, you should keep them as low as possible.


Reference

Nginx client_max_body_size documentation reference

client_max_body_size size;
Default:    client_max_body_size 1m;
Context:    http, server, location

Sets the maximum allowed size of the client request body, specified in the
“Content-Length” request header field. If the size in a request exceeds the configured
value, the 413 (Request Entity Too Large) error is returned to the client. Please be
aware that browsers cannot correctly display this error. Setting size to 0 disables
checking of client request body size.

One thought on “Fixing nginx 413 Request Entity Too Large”

Leave a Reply

Your email address will not be published. Required fields are marked *