413请求实体太大nginx django

ar7v8xwq  于 2024-01-06  发布在  Nginx
关注(0)|答案(4)|浏览(207)

我正在做一个实践Web服务(客户端的艺术书籍显示网站)客户端可以上传艺术书籍图像到服务器。
但是当客户端上传太多图像时,我会出现以下错误

  1. 413 Request Entity Too Large

字符串
我尝试在nginx.conf中添加client_max_body_size 100M;

  1. #user nobody;
  2. #Defines which Linux system user will own and run the Nginx server
  3. worker_processes 1;
  4. #error_log logs/error.log; #error_log logs/error.log notice;
  5. #Specifies the file where server logs.
  6. #pid logs/nginx.pid;
  7. #nginx will write its master process ID(PID).
  8. events {
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. #access_log logs/access.log main;
  15. sendfile on;
  16. server {
  17. listen 80;
  18. server_name xxxx.net;
  19. client_max_body_size 100M;
  20. keepalive_timeout 5;
  21. return 301 https://$server_name$request_uri;
  22. }
  23. # HTTPS server
  24. #
  25. server {
  26. listen 443 default_server ssl;
  27. server_name xxx.net;
  28. ssl_certificate /etc/letsencrypt/live/xxxx.net/fullchain.pem;
  29. ssl_certificate_key /etc/letsencrypt/live/xxxx.net/privkey.pem;
  30. ssl_session_cache shared:SSL:1m;
  31. ssl_session_timeout 5m;
  32. location / {
  33. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  34. proxy_set_header X-Forwarded-Proto https;
  35. proxy_set_header X-Real-IP $remote_addr;
  36. proxy_set_header HOST $http_host;
  37. proxy_set_header X-NginX-Proxy true;
  38. proxy_pass http://127.0.0.1:8000;
  39. proxy_redirect off;
  40. }
  41. }
  42. }


并尝试:

  1. sudo service nginx restart
  2. sudo service nginx reload


并重试

  1. runserver


但仍然得到

  1. 413 Request Entity Too Large


有人能帮忙吗?

zpjtge22

zpjtge221#

您已经修复了HTTP服务器上的问题,但您的HTTP服务器设置为301重定向到HTTPS服务器......您的HTTPS服务器没有配置client_max_body_size,因此它默认为1 M并导致此413(请求实体过大)错误。
要解决此问题,您只需将client_max_body_size添加到
Both
HTTP服务器块 * 和 * HTTPS服务器块,如下面的示例所示:

  1. http {
  2. ...
  3. ######################
  4. # HTTP server
  5. ######################
  6. server {
  7. ...
  8. listen 80;
  9. server_name xxxx.net;
  10. client_max_body_size 100M;
  11. ...
  12. }
  13. ######################
  14. # HTTPS server
  15. ######################
  16. server {
  17. ...
  18. listen 443 default_server ssl;
  19. server_name xxxx.net;
  20. client_max_body_size 100M;
  21. ...
  22. }
  23. }

字符串
有关client_max_body_size的更多信息,请访问:http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
Client_max_body_size大小;
默认值:client_max_body_size 1 m;
上下文:http、服务器、位置
设置客户端请求正文的最大允许大小,在“Content-Length”请求标头字段中指定。如果请求中的大小超过配置的值,则向客户端返回413(请求实体太大)错误。请注意,浏览器无法正确显示此错误。将size设置为0将禁用对客户端请求正文大小的检查。
在此了解有关配置HTTPS服务器的更多信息:http://nginx.org/en/docs/http/configuring_https_servers.html

展开查看全部
jqjz2hbq

jqjz2hbq2#

打开Ubuntu终端

使用nano文本编辑器:

$

  1. sudo nano /etc/nginx/nginx.conf

字符串

设置客户端主体大小为100M

  1. client_max_body_size 100M;

点赞:

  1. http {
  2. ##
  3. # Basic Settings
  4. ##
  5. client_max_body_size 100M;
  6. sendfile on;
  7. tcp_nopush on;
  8. tcp_nodelay on;
  9. keepalive_timeout 65;
  10. types_hash_max_size 2048;
  11. # server_tokens off;
  12. # server_names_hash_bucket_size 64;
  13. # server_name_in_redirect off;
  14. include /etc/nginx/mime.types;
  15. default_type application/octet-stream;
  16. ##
  17. # SSL Settings
  18. ## More codes here ...
  19. }

展开查看全部
sxpgvts3

sxpgvts33#

我知道这个问题已经得到了回答,但与其在服务器下的代码中多次使用“client_max_body_size 100 M;”,不如在http部分下添加一次--参见下面的第2行。

  1. http {
  2. client_max_body_size 100M;
  3. ...
  4. ######################
  5. # HTTP server
  6. ######################
  7. server {
  8. ...
  9. listen 80;
  10. server_name xxxx.net;
  11. ...
  12. }
  13. ######################
  14. # HTTPS server
  15. ######################
  16. server {
  17. ...
  18. listen 443 default_server ssl;
  19. server_name xxxx.net;
  20. ...
  21. }
  22. }

字符串
来源:http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

展开查看全部
vptzau2j

vptzau2j4#

这真的不相关,但我觉得正确的方法来处理文件验证是我要在这里演示。而不是使用nginx确定文件大小,你实际上可以在你的项目中用下面的过程来做,因为这就是我所做的,在我的项目中一切都很好。这样做的好处是,当用户上传无效文件时,你可以定制你想要发送回用户的消息。或文件的大小大于您在项目配置中指定的大小。

settings.py

  1. FILE_UPLOAD_MAX_MEMORY_SIZE = 3 * 1024 * 1024 # (3MEGABYTES)
  2. DATA_UPLOAD_MAX_MEMORY_SIZE = FILE_UPLOAD_MAX_MEMORY_SIZE

字符串

custom_validator.py

  1. def convert_to_megabyte(file_size):
  2. file_size_in_mb = round(file_size / (1000 * 1000))
  3. return ceil(file_size_in_mb)
  4. def custom_file_validator(file):
  5. file_types = ["image/png", "image/jpeg", "image/jpg", "application/pdf"]
  6. if not file:
  7. raise ValidationError("No file selected.....")
  8. if file.size > FILE_UPLOAD_MAX_MEMORY_SIZE:
  9. raise ValidationError(f"File shouldn't be larger than {convert_to_megabyte(FILE_UPLOAD_MAX_MEMORY_SIZE)}MB.")
  10. fs = FileSystemStorage()
  11. filename = fs.save(file.name, file)
  12. file_type = mimetypes.guess_type(filename)[0]
  13. if file_type not in file_types:
  14. raise ValidationError("Invalid file, please upload an image file with extensions (png, jpg or jpeg).")
  15. return file


然后你可以在你的django模型中添加custom_file_validor到file或者image字段

models.py

  1. class Company(models.Model):
  2. my_image = models.ImageField(upload_to="images/", blank=True, null=True,max_length=500, validators=[custom_file_validator])
  3. my_file = models.FileField(upload_to="files/", blank=True, null=True, max_length=500, validators=[custom_file_validator])


所以上面的代码总是基于你的自定义验证来验证镜像,而不是基于服务器(nginx)验证。
在你的nginx.conf上,只要把它添加到你的http块中

nginx.conf

  1. client_max_body_size 0


如果您的用户将上传图像或文件在他们的结束,你可以简单地只是使用下面的对您的看法。

views.py

  1. if request.method == 'POST' and request.FILES:
  2. document_type = request.POST.get("document-type", None)
  3. document = request.FILES.get("document", None)
  4. if document.size > base.FILE_UPLOAD_MAX_MEMORY_SIZE:
  5. messages.warning(
  6. request, f"Document cannot be larger than {convert_to_megabyte(base.FILE_UPLOAD_MAX_MEMORY_SIZE)}MB.")
  7. return redirect("create_company")
  8. fs = FileSystemStorage()
  9. filename = fs.save(document.name, document)
  10. file_type = mimetypes.guess_type(filename)[0]
  11. if file_type not in file_types:
  12. messages.info(request, "Invalid file uploaded, please upload an image (jpeg, png, jpg only).")
  13. return redirect('create_company')


所以上面将使用您的自定义验证,而不是服务器(nginx)验证,因为您已经将其设置为0。我希望这对将来看到这一点的人来说足够清楚。这花了我一些时间来弄清楚,但效果很好。

展开查看全部

相关问题