Nginx未能从Node/Express app发送完整文件

时间:2022-08-10 17:13:23

Sending large files (1.5gb) to a client with a connection speed < 2mbps results in the browser only receiving 1.08gb of data, but believing the download to be complete. Faster connections receive the full 1.5gb file.

将大文件(1.5gb)发送给一个连接速度小于2mbps的客户端只会收到1.08gb的数据,但相信下载是完整的。更快的连接接收完整的1.5gb文件。

My Express.js app figures out which file to send and responds with the response#download method:

我的表达。js应用程序通过响应#下载方法确定发送和响应的文件:

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);
});

Notice that I set the X-Accel-Redirect header to leverage NginxXsendfile

注意,我设置了x - accel重定向头,以利用NginxXsendfile。

My Nginx configuration:

我的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 个解决方案

#1


3  

I believe the original issue stems from Node (probably sending large files inside Node's I/O loop), and my assumption that Nginx was taking the download from Node properly was incorrect. I had a couple mistakes that meant NginxXSendfile wasn't working, and Node was still handling the response.

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

I had a syntax error:
To set response headers this is the correct syntax:

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

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

The body of the response should not be set when using the above header (doh!).
With Express/Connect/Node just #send the response with the X-Accel-Redirect header and set the Content-Disposition with response#attachment:

在使用上面的头(doh!)时,不应该设置响应的主体。使用Express/Connect/Node,只需#使用X-Accel-Redirect头发送响应,并使用response#附件设置内容配置:

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

#2


0  

I had to add a Content-Disposition to response headers in addition to X-Accel-Redirect

除了X-Accel-Redirect之外,我还必须为响应头添加内容配置

Express.js code:

表达。js代码:

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

Nginx congif file (/etc/nginx/sites-available/default)

Nginx congif文件(/ etc / Nginx /网站/默认)

server {

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

This serves the file test.txt, in /home/user/files (i.e. /home/user/files/test.txt).

这服务于文件测试。txt,在/home/user/files(即/home/user/files/test.txt)中。

#1


3  

I believe the original issue stems from Node (probably sending large files inside Node's I/O loop), and my assumption that Nginx was taking the download from Node properly was incorrect. I had a couple mistakes that meant NginxXSendfile wasn't working, and Node was still handling the response.

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

I had a syntax error:
To set response headers this is the correct syntax:

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

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

The body of the response should not be set when using the above header (doh!).
With Express/Connect/Node just #send the response with the X-Accel-Redirect header and set the Content-Disposition with response#attachment:

在使用上面的头(doh!)时,不应该设置响应的主体。使用Express/Connect/Node,只需#使用X-Accel-Redirect头发送响应,并使用response#附件设置内容配置:

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

#2


0  

I had to add a Content-Disposition to response headers in addition to X-Accel-Redirect

除了X-Accel-Redirect之外,我还必须为响应头添加内容配置

Express.js code:

表达。js代码:

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

Nginx congif file (/etc/nginx/sites-available/default)

Nginx congif文件(/ etc / Nginx /网站/默认)

server {

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

This serves the file test.txt, in /home/user/files (i.e. /home/user/files/test.txt).

这服务于文件测试。txt,在/home/user/files(即/home/user/files/test.txt)中。