下载大文件 不经过php 直接让apache发送文件给客户端 mod_xsendfile

最近有一个新的需求

  提供用户下载系统的镜像文件(1.6G的一个zip文件)

1 首先用了很挫的php读取文件的方式

  一下载整个server就blocked在那边

2 去看了下range

  发现它是用来实现断点续传的,也可以把大文件分成好几段传

似乎可以解决问题 试了下 还是不太好用

3 最后尝试了mod_xsendfile

  很好地解决了这个问题

1) 安装apache模块扩展工具apxs

sudo apt-get install apache2-dev 

2) 下载源码 mod_xsendfile.c

https://tn123.org/mod_xsendfile/
3) 编译源码

sudo apxs2 -cia mod_xsendfile.c 

4) 开启

sudo a2enmod xsendfile

5) 指定文件路径

<Directory /var/www/somesite/>
  XSendFilePath /var/www/shared
</Directory>

6)重启apache

sudo /etc/init.d/apache2 restart
// 或者
sudo service apache2 restart

7)输出case:

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

 

 

ps:后知后觉的发现 这里面应该是两个问题

1)php读取和输出大文件 太占内存 (用range分割文件进行读取发送)

2)php的阻塞问题 (下载前把session关掉应该就可以解决阻塞的问题 session_write_close )

等有空了 再试下这个

不过从感觉上 觉得x-sendfile应该更好用

 

参考资料:https://tn123.org/mod_xsendfile/

 

posted @ 2015-01-26 15:36  Ateoa  阅读(377)  评论(0编辑  收藏  举报