Nginx早期切断静态文件下载

Eli*_*Eli 6 python nginx flask uwsgi x-accel-redirect

我有一个Flask应用程序,通过x-accel-redirect将应该提供静态文件的请求重定向到NGINX.有时,这些下载将在完成之前被切断.例如,通过cURL,我会看到:

curl http://my_server/some_static_file.tar > temp.tar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
 77 14.4G   77 11.2G    0     0  55.8M      0  0:04:24  0:03:25  0:00:59 58.9M
curl: (18) transfer closed with 3449105332 bytes remaining to read
Run Code Online (Sandbox Code Playgroud)

这似乎更常见于非常大的文件(10gb +),但我已经看到它也发生在~90mb的较小文件上.Nginx访问日志显示来自和提供不同,不完整数据量的请求:

1.2.3.4 - - [18/Apr/2017:01:16:26 +0000] "GET /some/flask/static/file/path HTTP/1.1" 200 15146008576 "-" "curl/7.38.0" "5.6.7.8"
1.2.3.5 - - [18/Apr/2017:01:16:29 +0000] "GET /some/flask/static/file/path HTTP/1.1" 200 15441739776 "-" "curl/7.38.0" "6.7.8.9"
Run Code Online (Sandbox Code Playgroud)

errors.log 没什么用的.

我的相关烧瓶配置如下:

response = make_response('')
response.headers.set('X-Accel-Redirect', '/_special_nginx_path/' + file_name)
response.headers.set('Content-Disposition', 'attachment',
                     filename=file_name)
# have tried both with and without setting content-length
response.headers.set('Content-Length', os.path.getsize(file_path))
try:
    response.mimetype = mimetypes.guess_type(file_name)[0]
    if not response.mimetype:
        response.mimetype = 'application/octet-stream'
except AttributeError:
    response.mimetype = 'application/octet-stream'
return response
Run Code Online (Sandbox Code Playgroud)

我的相关NGINX配置如下(运行我的烧瓶应用程序的uWSGI服务器运行在127.0.0.1:1234):

location / {
            proxy_pass http://127.0.0.1:1234;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }



location /_special_nginx_path {
           internal;
           alias /path/to/static/files;
    }
Run Code Online (Sandbox Code Playgroud)

Tho*_*ohn 1

请检查你的磁盘使用情况,可能是由于这个原因造成的,首先检查nginx错误日志,错误日志可能有如下日志:

\n\n
\n

2018/10/28 14:20:24 [crit] 5432#5432: *75 pwritev() "/var/lib/nginx/uwsgi/1/00/0000000001" 读取时失败(28:设备上没有剩余空间)上游的,

\n
\n\n

首先,确定哪个分区没有可用空间。您可以通过在终端中键入以下命令来执行此操作:

\n\n
df -h\n
Run Code Online (Sandbox Code Playgroud)\n\n

您\xe2\x80\x99 现在将在屏幕上看到以下详细信息:

\n\n
\n

文件系统。尺寸。用过的。可用的。用过的。镶嵌在。

\n
\n\n

查看分区详细信息,检查是否有分区\xe2\x80\x99s磁盘空间使用率达到100%。

\n\n

找到分区后,将其打开并删除无用的文件,以释放磁盘空间并解决问题。

\n\n

如果分区已挂载到系统内存(由 TMPFS 目录表示),请运行以下命令将其卸载。

\n\n

卸载 path_to_the_directory。

\n\n

现在,重新启动 Nginx。该错误现在将从文件中消失。

\n\n

为了防止将来出现 no space left on device 错误,请编辑 Nginx 配置文件(或您的 website\xe2\x80\x99s 配置文件)并增加 key zone 的值。

\n\n

用户面临这个问题是因为他们将操作系统配置为从 RAM 提供缓存文件。虽然这可以快速提高站点的性能,但它会减少服务器上运行的其他应用程序可用的 RAM 量,并导致内存不足错误。

\n\n

如果您的服务器使用 SSD 而不是 HDD,则无需将分区挂载到系统内存中。

\n\n

感谢博客对我的帮助...

\n