<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post/5383/</link>
<title><![CDATA[奇妙的Apache 2.x  mod_xsendfile]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 04 Jul 2012 13:19:18 +0000</pubDate> 
<guid>http://jackxiang.com/post/5383/</guid> 
<description>
<![CDATA[ 
	日常应用中，我们经常会在php里面用到基于文件系统直接输出一个文件的方法，例如：<br/> <br/>//////////////////////////////////代码一///////////////////////////////<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#92;$fp = @fopen(&#92;$src_path, &quot;rb&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($fp === false)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(“bad file”);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;header(&quot;Content-Length: &quot; . filesize($src_path));<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;fpassthru($fp);<br/>&nbsp;&nbsp; <br/>fclose($fp);<br/> <br/>///////////////////////////代码一/////////////////////////////////////<br/> <br/>在Linux 2.4+内核版本里面，有了一种新的文件输出方法，那就是sendfile()。在2.6内核里面升级为了 linux-aio-sendfile。Sendfile()的方法是属于内核中的操作，”能把一个文件从特定部分开始的特定块通过一个socket发送出去，从而避免了多次调用read和write的内核上下文和用户上下文切换的开销，并且由于其内部实现利用了mmap技术，也减少了内存的复制开销”，速度比文件系统层的read要快上N++倍。<br/> <br/>在Lighttpd中，首次出现了通过程序通知服务器基于X-sendfile的方法，即：<br/> <br/>Php程序：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; header(&quot;X-Sendfile: /data1/i_m_a_send_file.html&quot;);<br/> <br/>Lighttpd:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 调用 Sendfile() 发送 /data1/i_m_a_send_file.html<br/> <br/>由于lighttpd的这个特性，使得其纯静态文件服务器的性能有了一个质的飞跃。<br/> <br/>同样上述方法在Nginx中对应的是：X-Accel-Redirect，叫法不一样，干的是同一件事情。<br/> <br/>在apache 2.x版本中，在编译的时候，apache也会检测内核版本，然后决定是否支持sendfile()的方法，也就是说只要2.4以上的内核版本，apache 2.x里面也是支持sendfile()的。不过apache本身并没有提供给cgi任何内部接口给php使用，于是有人写了一个模块，名字就叫作&nbsp;&nbsp;apahce 2 mod_xsendfile(http://tn123.ath.cx/mod_xsendfile/)，可以让apache 2平台上通过cgi header的方法通知apache调用sendfile()去发送某一个文件，从而大大降低系统的资源消耗和提高内容传输的响应速度。<br/> <br/>编译方法：libtool的版本为1.5.6版本<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /data1/apache/bin/apxs -i -a -c&nbsp;&nbsp;mod_xsendfile.c<br/> <br/>Httpd.conf 配置方法：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LoadModule xsendfile_module&nbsp;&nbsp; modules/mod_xsendfile.so<br/>XSendFile on<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSendFileAllowAbove on<br/> <br/>使用方法：我们将代码一简化为一行：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ////////////////////////代码二////////////////////////<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; header(&quot;X-Sendfile: &#123;$src_path&#125;&quot;);<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //////////////////////代码二/////////////////////////<br/><br/>模块下载地点：https://tn123.org/mod_xsendfile/<br/><br/>中文下载参考：http://www.laruence.com/2012/05/02/2613.html<br/>利用Nginx的X-Accel-Redirect头实现下载控制:http://www.zhuye123.com/Article/255/257/258/2011/2011032376195_2.html
]]>
</description>
</item><item>
<link>http://jackxiang.com/post/5383/#blogcomment63778</link>
<title><![CDATA[[评论] 奇妙的Apache 2.x  mod_xsendfile]]></title> 
<author>hxngb2lf &lt;xhtl311@domozmail.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 11 Jul 2012 01:29:58 +0000</pubDate> 
<guid>http://jackxiang.com/post/5383/#blogcomment63778</guid> 
<description>
<![CDATA[ 
	好的，真长见识了，说得很有道理
]]>
</description>
</item>
</channel>
</rss>