【问题标题】:Slower downloads through Apache than with PHP readfile通过 Apache 的下载速度比使用 PHP readfile 慢
【发布时间】:2013-10-26 01:54:45
【问题描述】:

我已经在我的服务器上使用 PHP 设置了一个下载脚本,它会在让用户通过 Apache (X-Sendfile) 下载文件之前检查一些细节。文件位于 Document-Root 之外。

使用 Apache 和 Module X-Sendfile 下载的代码是:

header("X-Sendfile: $fullpath");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$link_file\"");

使用 Apache 和 X-Sendfile 时,我的客户端下载速度为 500 kB/s。我还使用 Apache 和没有 X-Sendfile 的文档根目录中的相同文件对其进行了测试 - 这里也是一样!

所以我测试了几秒钟后通过带有 readfile 的 PHP 下载相同的文件,使用相同的客户端,两端相同的基础设施和相同的互联网连接:

header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($fullpath)));
header("Content-Disposition: attachment; filename=\"$link_file\"");
readfile($fullpath);

这次的下载速度是9.500 kB/s

我多次使用这两个选项重复此测试,每次尝试的结果都是相同的。尝试使用 PHP readfile 方法时,除了下载速度之外,唯一的区别是等待时间为几秒钟(取决于下载的文件的大小)。当立即重复 PHP readfile 方法时,等待时间没有再次出现。很可能是因为它是在第一次之后存储在内存中的。

我在服务器上使用了专业的 HP Raid-System,其平均本地速度为 800 MB/s,因此原因不能是 Diskspeed。我也没有在 Apache 的 httpd.conf 中找到任何压缩或带宽设置。

谁能解释一下为什么下载速度会有如此大的差异以及如何改变这种差异?

提前谢谢你。

  • 服务器:Windows Server 2008 R2 Apache/2.2.21 (Win32) PHP/5.4.20
  • 客户端:Windows 7 Ultimate x64 Google Chrome 30.0.1599.101 LAN >100 Mbit/s

【问题讨论】:

    标签: php performance apache readfile x-sendfile


    【解决方案1】:

    解决方案:

    httpd.conf,开启“EnableSendfile off”这一行

    【讨论】:

    • 出于好奇,您完成此操作后发送文件的速度是多少?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2022-12-03
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2016-12-18
    相关资源
    最近更新 更多