到服务器的AJAX POST导致错误413:请求实体太大

时间:2018-08-28 21:01:29

标签: django gunicorn cookiecutter-django

在JS插件中,我的Django视图接受POST编码图像的AJAX base64。问题是图像太大。我收到以下错误。

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

有关如何解决此问题的任何想法?我找到了nginx的解决方案,但是我正在gunicorn项目中使用cookiecutter-django

2 个答案:

答案 0 :(得分:2)

免责声明我强烈建议配置Nginx服务器以将代理传递给Gunicorn ...

似乎nginx的{​​{1}}参数可能设置为低两个值,以便处理后的图像可以毫无问题地发布到服务器。我讨厌服务器配置,但是您需要编辑client_max_body_size配置。感觉可以通过在nginx config中的http {..}块中添加以下行来轻松解决:

nginx

这甚至可能位于您站点的nginx配置文件-http { #... client_max_body_size 100m; client_body_timeout 1000s; #... } 中的服务器块中:

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
    }
}

免责声明:请向服务器专家寻求专业建议! :)

答案 1 :(得分:1)

弄清楚了。对于100mb以上的图像,Django的默认值太低。

必须更改

的设置
DATA_UPLOAD_MAX_MEMORY_SIZE = XXXX
FILE_UPLOAD_MAX_MEMORY_SIZE = XXXX