Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

413 Request Entity Too Large nginx django

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?

like image 386
Jade Han Avatar asked May 03 '16 02:05

Jade Han


People also ask

How do I fix Nginx 413 Request Entity Too Large?

The solution is to increase the client request size body by the client_max_body_size parameter in nginx. conf file. Following is the description of the client_max_body_size parameter. The default value is 1MB and you can set this to an HTTP, server or location contexts.

How does Django handle 413 error?

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 { ...

What is 413 Request Entity Too Large?

What does “413 Request Entity Too Large” mean? A 413 HTTP error code occurs when the size of a client's request exceeds the server's file size limit. This typically happens when a client attempts to upload a large file to a web server, and the server responds with a 413 error to alert the client.

What is 413 request entity too large in Django?

Python 3.7 + Django 2 + Nginx 1.10.3 If you want to upload a large file of which size is more than 1 MB to Django application (Django + uWSGI + Nginx), you may get ‘413 Request Entity Too Large problem’. This is because in Nginx, the default maximum accepted body size of client request is 1 MB.

How to configure nginx to allow upload size larger than required?

Nginx can be set to allow the maximum size of the client request body using client_max_body_size directive. If the size of a request exceeds the configured value, the 413 (Request Entity Too Large) error returned to the client. You will see an error as follows: You need to configure both nginx and php to allow upload size.

How to solve the 413 issue in Nginx?

Adding nginx.ingress.kubernetes.io/proxy-body-size: xm can help to solve 413 issue. Play around with the sizes a bit to find which ones work best for you. Looking for something else?

How to fix Django error 400 when uploading large files?

After increasing the limit in Nginx, if you upload a large file of which size is more than 2.5 MB to Django application, you may get 400 error with the following error message: To fix this bug, you need to set DATA_UPLOAD_MAX_MEMORY_SIZE in settings.py.


2 Answers

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

like image 185
Joshua Craven Avatar answered Sep 19 '22 13:09

Joshua Craven


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 ...  } 
like image 34
MdNazmulHossain Avatar answered Sep 20 '22 13:09

MdNazmulHossain