【发布时间】: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