0

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.

Emile
  • 3,464
  • 8
  • 46
  • 77
  • Hi Emile, what is the size (in bytes) of the `base64` encoded image? You will need to specify default limits for nginx to process larger file sizes. –  Aug 28 '18 at 21:09
  • Working on finding an image size now... unfortunately I'm using gunicorn, not nginx – Emile Aug 28 '18 at 21:15
  • Hi Emile - use Nginx as your proxy server and the below is absolutely valid - I'd go with - section 2.6.1 of these docs: https://media.readthedocs.org/pdf/gunicorn-docs/19.6.0/gunicorn-docs.pdf (You're porbably using Nginx as your proxy - but you don't realise it!) –  Aug 28 '18 at 21:24
  • They are 100+ KB images – Emile Aug 28 '18 at 21:24
  • Look into setting up Nginx as your proxy for your Gunicorn server. –  Aug 28 '18 at 21:25
  • This tutorial has always served me well: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04#configure-nginx-to-proxy-pass-to-gunicorn –  Aug 28 '18 at 21:26

2 Answers2

3

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! :)

1

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
Emile
  • 3,464
  • 8
  • 46
  • 77