Nginx未能从Node/Express应用程序发送完整文件

3
将大文件(1.5gb)发送给连接速度<2mbps的客户端会导致浏览器只接收到1.08gb的数据,但认为下载已完成。更快的连接可以接收完整的1.5gb文件。 我的Express.js应用程序找出要发送的文件,并使用response#download方法进行响应:
app.get('/download-the-big-file', function(request, response) {
  var file = {
    name: 'awesome.file',
    path: '/files/123-awesome.file'
  };

  response.header("X-Accel-Redirect: " + file.path);

  response.download(file.path, file.name);
});

注意,我设置了X-Accel-Redirect头部来利用NginxXsendfile
我的Nginx配置:
server {

    client_max_body_size 2g;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8000/;
    }

    location /files {
        root /media/storage;
        internal;
    }
}
2个回答

4

我相信最初的问题源于Node(可能在Node的I/O循环中发送大文件),而我假设Nginx正确获取了从Node下载的内容是错误的。我犯了一些错误,导致NginxXSendfile无法正常工作,而Node仍然在处理响应。

我有一个语法错误:
设置响应头部需要使用正确的语法:

response.header('X-Accel-Redirect', file.path);

响应体在使用上述标头时不应设置(哎呀!)。对于 Express/Connect/Node,只需将响应发送到 X-Accel-Redirect 标头,并使用 response#attachment 设置 Content-Disposition:
response.attachment(file.name);
response.send();

0

我必须在响应头中添加Content-Disposition以及X-Accel-Redirect

Express.js 代码:

app.get('/files/test.txt', function (req, res) {
  res.setHeader('X-Accel-Redirect', '/files/test.txt');
  res.attachment('text.txt');
  res.send();
});

Nginx配置文件(/etc/nginx/sites-available/default)
server {

    location /files {
      internal;
      root   /home/user;
    }
}

这将提供位于/home/user/files(即/home/user/files/test.txt)的test.txt文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接