带上传模块的Nginx不允许我上传比client_body_buffer_size更大的文件

时间:2014-03-25 22:49:35

标签: nginx configuration upload buffer

我正在尝试通过Nginx上传模块上传文件。它工作正常,文件上传,上传完成后,请求被代理到我的节点应用程序。

唯一的问题是,如果我上传文件(比方说图像),我的图像中只有N个第一个字节是正确的,其余的是黑色像素。我发现N是client_body_buffer_size的值。

这是我对这个虚拟主机的讨论:

server {

listen 80;
server_name example.com;

access_log /var/log/nginx/example.access.log;
error_log  /var/log/example.nginx_error.log debug;

client_max_body_size 4G;

location / {
    proxy_pass http://localhost:3112;
    add_header 'Access-Control-Allow-Origin' "$http_origin";
}

location /uploadNginx {
  # For CORS
  if ($request_method = OPTIONS) {
    add_header Pragma no-cache;
  if ($request_method = OPTIONS) {
    add_header Pragma no-cache;
    add_header X-Content-Type-Options nosniff;

    # Access control for CORS
    add_header Access-Control-Allow-Origin "$http_origin";
    add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
    add_header Access-Control-Allow-Headers "cache-control, content-range, accept, origin, session-id, content-disposition, x-requested-wit$
    add_header Access-Control-Allow-Credentials "true";

    # 10 minute pre-flight approval
    add_header Access-Control-Max-Age 600;

    return 204;
  }

  if ($request_method = POST) {
    add_header Pragma no-cache;
    add_header X-Content-Type-Options nosniff;
    add_header Pragma no-cache;
    add_header X-Content-Type-Options nosniff;
    #add_header Cache-control "no-story, no-cache, must-revalidate";

    # Access control for CORS
    add_header Access-Control-Allow-Origin "$http_origin";
    add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
    add_header Access-Control-Allow-Headers "cache-control, content-range, accept, origin, session-id, content-disposition, x-requested-wit$
    add_header Access-Control-Allow-Credentials "true";

    # 10 minute pre-flight approval
    add_header Access-Control-Max-Age 600;

    # Set specified fields in request body
    # This puts the original filename, new path+filename and content type
    # in the requests params
    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_set_form_field $upload_field_name.content_type "$upload_content_type";
    upload_set_form_field $upload_field_name.path "$upload_tmp_path";

    upload_pass_form_field "^X-Progress-ID$|^authenticity_token$";
    upload_cleanup 400 404 499 500-505;

    # Upload general conf
    upload_buffer_size 512k;
    upload_resumable off;
  }

  # Store files to this directory
  # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
  upload_store /var/tmp/nginx_uploads 1;

  # The upload_state folder is where your .state files will live. These files are used to track
  # the progress of chunked uploads. Note that you do not need a "1" here, as they all go into
  # the same folder.
  upload_state_store /var/upload_state;

  # set permissions on the uploaded files
  upload_store_access user:rw group:rw all:r;

  upload_pass_form_field "^submit$|^description$";

  upload_pass                @fast_upload_endpoint;
}

location @fast_upload_endpoint {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_pass_header 'Access-Control-Allow-Origin';
  proxy_pass http://localhost:3112;
}
}

上传看起来需要正确的上传时间(即使缓冲区大小为8k,2M图像的上传时间也需要几秒钟),所以我猜全部图片已上传,但似乎没有正确收集。关于它的任何提示?

1 个答案:

答案 0 :(得分:0)

如果您的client_body_buffer_size小于请求的正文大小,则整个正文或部分将写入磁盘。它可能会导致数据损坏,但很少。