Emile
Emile

Reputation: 3474

AJAX POST to server results in Error 413: Request Entity Too Large

In a JS plugin, my Django view accepts an AJAX POST of a base64 encoded image. The problem is that the images are too large. I'm getting the following error.

django_1    | Traceback (most recent call last):
django_1    |   File "/usr/local/lib/python3.6/site-packages/raven/transport/threaded.py", line 165, in send_sync
django_1    |     super(ThreadedHTTPTransport, self).send(url, data, headers)
django_1    |   File "/usr/local/lib/python3.6/site-packages/raven/transport/http.py", line 43, in send
django_1    |     ca_certs=self.ca_certs,
django_1    |   File "/usr/local/lib/python3.6/site-packages/raven/utils/http.py", line 66, in urlopen
django_1    |     return opener.open(url, data, timeout)
django_1    |   File "/usr/local/lib/python3.6/urllib/request.py", line 532, in open
django_1    |     response = meth(req, response)
django_1    |   File "/usr/local/lib/python3.6/urllib/request.py", line 642, in http_response
django_1    |     'http', request, response, code, msg, hdrs)
django_1    |   File "/usr/local/lib/python3.6/urllib/request.py", line 570, in error
django_1    |     return self._call_chain(*args)
django_1    |   File "/usr/local/lib/python3.6/urllib/request.py", line 504, in _call_chain
django_1    |     result = func(*args)
django_1    |   File "/usr/local/lib/python3.6/urllib/request.py", line 650, in http_error_default
django_1    |     raise HTTPError(req.full_url, code, msg, hdrs, fp)
django_1    | urllib.error.HTTPError: HTTP Error 413: Request Entity Too Large

Any ideas on how to resolve this? I have found solutions with nginx, however I am using gunicorn within cookiecutter-django project.

Upvotes: 0

Views: 4066

Answers (2)

Emile
Emile

Reputation: 3474

Figured it out. Django's defaults are too low for 100mb+ images.

Had to change my settings for

DATA_UPLOAD_MAX_MEMORY_SIZE = XXXX
FILE_UPLOAD_MAX_MEMORY_SIZE = XXXX

Upvotes: 1

user10261970
user10261970

Reputation:

Disclaimer: I would strongly advise configuring an Nginx server to Proxy Pass to Gunicorn...

It seems that nginx's client_max_body_size parameter is probably set two low for the processed image to be posted to the server without issue. I hate server configurations, but you'll need to edit yout nginx configurtion. It feels like it could be solved easily by adding following lines to http{..} block in nginx config:

http {
    #...
    client_max_body_size 100m;
    client_body_timeout 1000s;
    #...
} 

This may even be in the server block in your site's nginx config file - yoursite_nginx.conf:

server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 100M;   # adjust to taste

    # max timeout duration
    client_body_timeout 1000s;  # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

Please note you'll also need to run the following on the server for the changes to take effect:

$ service nginx reload

Disclaimer: please seek professional advise from a server expert! :)

Upvotes: 3

Related Questions