[Django]-413 Request Entity Too Large nginx django

63👍

You’ve fixed the issue on your HTTP server, but your HTTP server is set to 301 redirect to your HTTPS server… your HTTPS server does not have client_max_body_size configured, so it is defaulting to 1M & causing this 413 (Request Entity Too Large) error.

To fix this issue, you simply need to add client_max_body_size to BOTH the HTTP server block and the HTTPS server block, as shown in the example below:

http {
    ...
    ######################
    # HTTP server
    ######################
    server {
        ...
        listen       80;
        server_name  xxxx.net;
        client_max_body_size 100M;
        ...
    }

    ######################
    # HTTPS server
    ######################
    server {
        ...
        listen       443 default_server ssl;
        server_name  xxxx.net;
        client_max_body_size 100M;
        ...
    }
}

More info on client_max_body_size here: http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

Syntax: 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.

Read More about configuring HTTPS servers here: http://nginx.org/en/docs/http/configuring_https_servers.html

5👍

Open the terminal for Ubuntu

Use nano text editor:

$

sudo nano /etc/nginx/nginx.conf

set client body size to 100M

client_max_body_size 100M;

Like:

http {

        ##
        # Basic Settings
        ##
        client_max_body_size 100M;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ## More codes here ...

}

1👍

I know this has already been answered, but rather than having the "client_max_body_size 100M;" multiple times in your code under the servers, you can just add it once, under the http section – see line 2 below.

http {
    client_max_body_size 100M;
    ...
    ######################
    # HTTP server
    ######################
    server {
        ...
        listen       80;
        server_name  xxxx.net;
        ...
    }

    ######################
    # HTTPS server
    ######################
    server {
        ...
        listen       443 default_server ssl;
        server_name  xxxx.net;
        ...
    }
}

Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

👤ctay

Leave a comment