【问题标题】:Nginx cuts off static files downloads earlyNginx 提前切断静态文件下载
【发布时间】:2017-09-14 23:40:25
【问题描述】:

我有一个 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

这似乎更常发生在非常大的文件 (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"

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

我的相关 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;
    }

【问题讨论】:

    标签: python nginx flask uwsgi x-accel-redirect


    【解决方案1】:

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

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

    首先,确定哪个分区没有可用空间。您可以通过在终端中输入以下命令来实现:

    df -h
    

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

    文件系统。尺寸。用过的。可用的。用过的。安装在上面。

    查看分区详情,查看是否有分区的磁盘空间使用率达到 100%。

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

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

    卸载 path_to_the_directory。

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

    为防止以后出现 no space left on device 错误,编辑 Nginx 配置文件(或您网站的配置文件)并增加 key zone 的值。

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

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

    感谢blog 对我的帮助...

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2018-10-23
      • 1970-01-01
      • 2016-01-02
      • 2021-09-03
      • 2018-05-18
      • 2015-07-30
      • 2021-12-02
      • 2020-09-14
      相关资源
      最近更新 更多