【问题标题】:Configure Nginx for file upload using connect-busboy使用 connect-busboy 配置 Nginx 进行文件上传
【发布时间】:2017-05-15 06:18:53
【问题描述】:

我在 NodeJS 中有一个 API,我可以在其中上传文件,并使用 connect-busboy 包在服务器端接收它。

因此,例如,这里是处理请求的部分代码:

var app = require('express')();
var busboy = require('connect-busboy');

app.use(busboy({
        highWaterMark: 2 * 1024 * 1024,
        limits: {
            fileSize: 1024 * 1024 * 1024 // 1 GB
        },
        immediate: true
    }));

var busboyHandler = function (req, res) {
    if (req.busboy) {
        req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
            console.log('received file ', req.path, fieldname, file, filename, encoding, mimetype);
        });

        req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
            console.log('field..', key, value, keyTruncated, valueTruncated);
        });

        req.busboy.on('finish', function() {
            console.log('busboy finished');
        });
    }
};

app.post('api/documents/files', busboyHandler); 

当我使用npm start 启动 API 并将文件直接上传到此 API 时,这很有效,但是,当我配置 Nginx Docker 时,它适用于非常小的文件,但对于大多数文件,它们不会上传成功。

这是我的nginx.conf 文件的摘录:

user nobody nogroup;
worker_processes auto;          # auto-detect number of logical CPU cores

events {
  worker_connections 512;       # set the max number of simultaneous connections (per worker process)
}

http {
  include mime.types;

  client_max_body_size 100M;
  client_body_buffer_size 256k;

  upstream api_doc {
    server 192.168.2.16:4956;
  }

  server {
    listen *:4000;                # Listen for incoming connections from any interface on port 80
    server_name localhost;             # Don't worry if "Host" HTTP Header is empty or not set
    root /usr/share/nginx/html; # serve static files from here

    client_max_body_size 100M;
    client_body_buffer_size 256k;

    location /api/documents {
        proxy_pass http://api_doc;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
     }
  }

}

我看到了received file的日志,但是当它在Nginx下时,从来没有busboy finished的日志,不像在没有Nginx的情况下直接调用API。

我尝试更改这些 Nginx 配置,但没有成功:client_max_body_sizeclient_body_buffer_size。在我看来,API 只接收较大文件的文件块,而不是应有的整个文件或所有块。

任何帮助将不胜感激。

谢谢, 西蒙

【问题讨论】:

  • 为什么在这里投反对票?

标签: node.js nginx file-upload busboy


【解决方案1】:

原来问题出在其他地方,我在文件完全上传之前就开始读取传入的流,因此由于某些原因导致了传入的流中断。

【讨论】:

    猜你喜欢
    • 2015-11-17
    • 2016-05-08
    • 2014-07-30
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2023-03-05
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多