用x-sendfile解决discuz+php-cgi下的lighttpd内存占用暴增的问题

       discuz论坛中很多附近要下载,或图片附件要显示,这些附件都会被php-cg直接读给lighttpd,lighttpd会全部接收到内存,并且不会迅速释放,从而导致其内存占用增大,我一个中等访问量的论坛lighttpd占用内存有时会达到1.4G。

       x-sendfile可以解决这个问题!!php通过x-sendfile头告诉lighttpd文件的路径,lighttpd通过系统调用sendfile直接发送文件到客户端,这样避免了内存占用!!效果很明显!!

 

1、修改lighttpd配置:

"host" => "127.0.0.1",
 "port" => "3000",
 "broken-scriptfilename" => "enable",
 "allow-x-send-file" => "enable"
 

 

2、修改discuz的attachment.php:

 

if(!$attach['remote']) {
                $thumbfilename = $attachdir.'/'.$attach['attachment'].'.thumb.jpg';
                dheader("X-LIGHTTPD-send-file: " . $thumbfilename);
                exit;
                if(is_readable($thumbfilename)) {
                        $filesize = filesize($thumbfilename);
                        $fp = fopen($thumbfilename, 'rb');
                        @flock($fp, 2);
                        $attachment = @fread($fp, $filesize);
                        @fclose($fp);
                        echo $attachment;
                        exit;
                }
        } else {
                if(getremotefile($attach['attachment'].'.thumb.jpg')) {
                        exit;
                }
        }

 

if(!$attach['remote']) {
        error_reporting(0);
        dheader("X-LIGHTTPD-send-file: " . $filename);
        exit;
//      getlocalfile($filename, $readmod, $range);
} else {
        if(!getremotefile($attach['attachment'])) {
                showmessage('attachment_nonexistence');
        }
}

 

 

另外:这只能解决文件发送的内存占用问题,对动态生成的页面,如果页面比较大,仍然存在内存占用的问题,这个问题我另有文章说明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值