<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[刘新修]]></title> 
<link>http://pic1.liuxinxiu.com:80/index.php</link> 
<description><![CDATA[刘新修的个人博客 (Liuxinxiu'S Blog)]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[刘新修]]></copyright>
<item>
<link>http://pic1.liuxinxiu.com:80/nginx_limit/</link>
<title><![CDATA[Nginx简单的防盗链和带宽限制]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[web服务器]]></category>
<pubDate>Sun, 07 Nov 2010 02:51:09 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/nginx_limit/</guid> 
<description>
<![CDATA[ 
	<p>&nbsp;&nbsp;&nbsp; 很多时候，服务不是被用户流量击垮，而是被大量的对你没有任何贡献的盗链击倒，所以作为一个web站点防盗链是首先要考虑的问题，目前来说，对于各个web服务器，简单的防盗链方法多数是做rewrite，判断referer是否有效，当然高端的伪造referer的情况不在这里讨论。<br /><br />&nbsp;&nbsp; &nbsp;&nbsp; 在apache下，防盗链的方法有很多，你可以看看apache的日志，有多少是外部直接referer过来的，有可能比内部引用还多，尤其是图片和下载类站点更加明显。在apache下，最简单的防盗链使用类似这个形式：<br /><br style="background-color: rgb(0,0,255)" /><font color="#ff0000" style="background-color: rgb(255,255,255)">SetEnvIfNoCase Referer &quot;^http://www.google.com&quot; local_ref=1<br />SetEnvIfNoCase Referer &quot;^http://google.com&quot; local_ref=1<br />&lt;filesmatch &quot;&#92;.(txt&#124;doc&#124;mp3&#124;zip&#124;rar&#124;jpg&#124;gif)&quot;&gt;<br />Order Allow,Deny<br />Allow from env=local_ref</font><br />或者在apache下使用RewriteEngine on，然后使用RewriteCond &#123;HTTP_REFERER&#125; 来定义，这些都是防止比较低级的盗链，如果是面对迅雷或者其他的话，这个远远不够，但是不是这里讨论的范围。<br /><br />&nbsp;&nbsp;&nbsp; 对于nginx而言，本身也有简单的防盗链模块ngx_http_referer_module，配置比较简单，定义文件类型：<br /><font color="#ff0000"><span style="background-color: rgb(255,255,255)"><br />location ~ .*&#92;.(gif&#124;jpg&#124;jpeg&#124;png&#124;bmp&#124;swf)$ &#123;</span><br style="background-color: rgb(255,255,255)" /><span style="background-color: rgb(255,255,255)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; valid_referers none blocked server_names *.163.com 163.com baidu.com； </span><br style="background-color: rgb(255,255,255)" /><span style="background-color: rgb(255,255,255)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($invalid_referer) &#123;return 403;&#125;</span><br style="background-color: rgb(255,255,255)" /><span style="background-color: rgb(255,255,255)">&nbsp;&nbsp;&nbsp; expires&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 30d;</span><br style="background-color: rgb(255,255,255)" /><span style="background-color: rgb(255,255,255)">&#125;</span></font><br /><br />具体的可以参考这里：http://wiki.nginx.org//NginxHttpLimitZoneModule，同时还有一个第三的防盗链相关模块，ngx_http_accesskey_module：<br /><font color="#ff0000">location /download &#123;<br />accesskey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; on;<br />accesskey_hashmethod md5;<br />accesskey_arg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;key&quot;;<br />accesskey_signature&nbsp;&nbsp; &quot;mypass$remote_addr&quot;;<br />&#125;</font><br /><br />具体的使用方法：http://wiki.nginx.org//NginxHttpAccessKeyModule<br /><br /><br />&nbsp;&nbsp;&nbsp; 对于带宽限制，apache可以动态编译一些模块进去，mod_evasive20.so和mod_bw.so都是对防止简单的dos和带宽限制而存在的，而对于nginx，可以使用nginx的标准模块ngx_http_limit_zone_module，进行会话的并发连接数控制：<br /><span class="Apple-style-span" style="font-weight: normal; font-size: 14px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); text-indent: 0px; line-height: 28px; font-style: normal; white-space: normal; letter-spacing: normal; border-collapse: separate; text-align: left; font-variant: normal; orphans: 2; widows: 2">http &#123;<br />&nbsp;&nbsp;&nbsp; limit_zone&nbsp;&nbsp; one $binary_remote_addr 10m; <br />#</span><span class="Apple-style-span" style="font-weight: normal; font-size: 14px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); text-indent: 0px; line-height: 28px; font-style: normal; white-space: normal; letter-spacing: normal; border-collapse: separate; text-align: left; font-variant: normal; orphans: 2; widows: 2">定义一个叫&ldquo;one&rdquo;的记录区，总容量为 10M，以变量 $binary_remote_addr 作为会话的判断基准（即一个地址一个会话）</span></p><p><a target="_blank" href="http://at.liuxinxiu.com/2010/11/image/limit1.jpg"><img height="131" width="510" alt="" src="http://at.liuxinxiu.com/2010/11/image/limit1.jpg" /></a><br /><span class="Apple-style-span" style="font-weight: normal; font-size: 14px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); text-indent: 0px; line-height: 28px; font-style: normal; white-space: normal; letter-spacing: normal; border-collapse: separate; text-align: left; font-variant: normal; orphans: 2; widows: 2">&nbsp;&nbsp;&nbsp; ...<br />&nbsp;&nbsp;&nbsp; server &#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br /><font color="#ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; location /icons_rar/ &#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; limit_conn&nbsp;&nbsp; one 2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; limit_rate 2k;<br /></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;</span></p><p><a target="_blank" href="http://at.liuxinxiu.com/2010/11/image/limit2.jpg"><img height="307" width="563" alt="" src="http://at.liuxinxiu.com/2010/11/image/limit2.jpg" /></a><br /># 限制 /<span class="Apple-style-span" style="font-weight: normal; font-size: 14px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); text-indent: 0px; line-height: 28px; font-style: normal; white-space: normal; letter-spacing: normal; border-collapse: separate; text-align: left; font-variant: normal; orphans: 2; widows: 2"><font color="#000000">icons_rar/</font></span> 目录下：</p><p>limit_conn 一个会话只能进行两个连接。超过一个，则返回503。</p><p>imit_rate 来控制该目录的下载速度为2KB/S</p><p><strong># 如果限制当前server内域名下所有目录下载显示则写 /&nbsp; 如：</strong></p><p><br /><font color="#333333">&nbsp;&nbsp;&nbsp; server &#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br /></font><font color="#ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; location / &#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; limit_conn&nbsp;&nbsp; one 2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; limit_rate 2k;<br /></font><font color="#333333">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;</font></p><p><a target="_blank" href="http://at.liuxinxiu.com/2010/11/image/limit3.jpg"><img height="271" width="527" alt="" src="http://at.liuxinxiu.com/2010/11/image/limit3.jpg" /></a></p><p><strong>之后用迅雷下载文件测试：</strong></p><p><a target="_blank" href="http://at.liuxinxiu.com/2010/11/image/limit-down.jpg"><img height="62" width="693" alt="" src="http://at.liuxinxiu.com/2010/11/image/limit-down.jpg" /></a><br /><br />这是简单的nginx的方案，更高级的应用应该是在客户端类型或者根据日志分析后，针对具体问题做文章，例如对$http_user_agent的特殊内容进行匹配，然后返回503。<br /><br />为什么要返回503？如果直接返回403，有可能被下载工具发现，403的状态被认为被禁止了，然后进行调整继续作案。而返回一个503，对服务器来说影响不大，只占用一个nginx的线程而已。<br /><br />先说到这里，以后再继续补充。<br /><br />&nbsp;</p>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/nginx_limit/#blogcomment</link>
<title><![CDATA[[评论] Nginx简单的防盗链和带宽限制]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/nginx_limit/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>