node.js - Nginx 无法从 Node/Express 应用程序发送完整文件

标签 node.js nginx express

将大文件 (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 header 设置为利用 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;
    }
}

最佳答案

我认为最初的问题源于 Node(可能在 Node 的 I/O 循环中发送大文件),而我认为 Nginx 正确地从 Node 下载的假设是不正确的。我犯了几个错误,这意味着 NginxXSendfile 无法正常工作,而 Node 仍在处理响应。

我有一个语法错误:
要设置响应 header ,这是正确的语法:

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

当使用上述 header 时,不应设置响应的主体(doh!)。
使用 Express/Connect/Node 只需 #send 带有 X-Accel-Redirect header 的响应并使用 response#attachment 设置 Content-Disposition:

response.attachment(file.name);
response.send();

关于node.js - Nginx 无法从 Node/Express 应用程序发送完整文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10002518/

相关文章:

javascript - 我如何强制这个 promise 抛出特定的错误?

nginx - 如何在 IBM Bluemix/Kubernetes 中为代理缓冲参数添加自定义 NGINX(入口)片段?

node.js - fs.mkdir 和 fs.rename 的 NodeJs 方法链接

node.js - Sequelize 模型 - 删除 belongsToMany n :m association 的项目

node.js - NodeJS 中的提示模块重复输入

javascript - 如何访问以下数组中的 ID 和 pID?

nginx - 高负载下502网关错误(nginx/php-fpm)

http - 配置 nginx 以从单个服务器提供 HTTP 和 HTTPS 站点的麻烦

node.js - 如何保护 express.js 中的路由?

javascript - express.index 发送 json 对象时出现问题