26

I am making a practice web service (client's artbook display web site) The client can upload artbook images to the server.

But I get the following error when the client uploads too many images

413 Request Entity Too Large

I tried adding client_max_body_size 100M; in nginx.conf

#user  nobody;
#Defines which Linux system user will own and run the Nginx server

worker_processes  1;

#error_log  logs/error.log; #error_log  logs/error.log  notice;
#Specifies the file where server logs.

#pid        logs/nginx.pid;
#nginx will write its master process ID(PID).

events {
    worker_connections  1024;
}


http {
    include       mime.types;

    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;

    server {
        listen       80;

        server_name  xxxx.net;
        client_max_body_size 100M;
        keepalive_timeout 5;

        return 301 https://$server_name$request_uri;

    }

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

        ssl_certificate      /etc/letsencrypt/live/xxxx.net/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/xxxx.net/privkey.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;


        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_pass http://127.0.0.1:8000;
            proxy_redirect off;
        }
    }
}

and tried:

sudo service nginx restart
sudo service nginx reload

and retry

runserver 

but still get

413 Request Entity Too Large

Can anybody help?

solarissmoke
  • 24,362
  • 12
  • 56
  • 60
Jade Han
  • 873
  • 2
  • 14
  • 29
  • Is nginx running on its own or is there some other platform (PHP, Python etc) that it is proxying for? – solarissmoke May 03 '16 at 04:06
  • 11
    Also you have added `client_max_body_size` to your HTTP server block but not to the HTTPS server block. That is probably where the issue is. – solarissmoke May 03 '16 at 04:56
  • 5
    I solved my problem, HTTPS **must have client_max_body_size** thank you @solarissmoke – Jade Han May 04 '16 at 04:21

1 Answers1

32

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

Joshua Craven
  • 2,610
  • 1
  • 22
  • 31