首页 > 解决方案 > 用于 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

标签: djangodockernginxfile-uploaddjango-rest-framework

解决方案


推荐阅读