django - 用于 Django REST 框架的 Nginx 上传模块
问题描述
我想使用 nginx 上传模块将大文件上传到我的 djangos MEDIA 文件夹。
单个文件上传的 Django REST“POST”端点位于
http://localhost:1337/api/record/measurements/#id/upload/
其中#id 可以是整数(1,2,3,4,5...),并且适用于没有 nginx 的 django。
我写了 nginx 配置
#/etc/nginx/conf.d/default.conf
location @backend {
proxy_pass http://django-app:8000;
}
location /api/record/measurements/1/upload/ {
error_page 418 = @upload;
if ($request_method = POST ) {
return 418;
}
include /etc/nginx/mainloc.conf;
}
location @upload {
upload_pass @backend;
upload_store /vol/web/media/_upload 1;
upload_store_access user:r;
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
upload_pass_form_field "^submit$|^description$";
upload_cleanup 400 404 499 500-505;
}
location / {
include /etc/nginx/mainloc.conf;
}
和
#mainloc.conf
proxy_pass http://django-app:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
编辑:
我设法将文件上传到/vol/web/media/_upload,但是文件上传完成后,我得到了响应
{
"id": 1,
"data": null
}
并且上传的文件不会移动到最终位置 MEDIA=/vol/web/media/ 但仍保留在upload_store
解决方案
推荐阅读
- ios - 为什么无法将 `(()->Void)` 转换为 `(()->Void)?
- java - Liskov 的替换原则如何适用于 Object 等类?
- reactjs - 如何引起对重新渲染复选框的反应
- swift - 在 SwiftUI 中单击按钮时创建重复计数器
- ruby-on-rails - 为什么 http 域重定向而不是 https 域?
- python - 我可以看到两个日期之间的文件,但我也需要将它们复制并合并到另一个文件夹
- javascript - moment-timezone :从一个时区转换到另一个时区时没有得到想要的时间
- mongodb - Amazon Document DB 中的 Mongodb 聚合函数
- go - 在函数内设置空接口{}类型的变量的值
- ios - 为什么我们不能在 swiftUI Priview 中传递实例变量?