将大文件 (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/