<?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//</link>
<title><![CDATA[nginx防hashdos模块释出 ，nginx 防止小型ddos攻击 。ngx_http_limit_req_module 模块，这个可以限制每秒的请求，超过就返回 503 错误码。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 13 Jun 2012 00:34:37 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：nginx 请求php的9000端口也是一个问题，nginx就是客户端，实践发现类似shell 正常返回0，错误返回1，fastcgi的php也是一样，php有一个队列，backlog =sysctl_somaxconn;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//也就是说当backlog=-1时，在内核中backlog被赋值为/proc/sys/net/core/somaxconn 的值，本机上为262144，性能差是由于php-fpm backlog参数设置为-1，导致fpm没能及时取出完成连接队列的socket，出现SYN 超时，最终导致压不上去，表现出性能差。<br/><br/>所以安装php-fpm时backlog一定要重新设置，不能用fpm默认配置的-1 ，可以根据机器的并发量来设置，建议设置在1024以上，最好是2的幂值（因为内核会调整成2的n次幂）。而只要这个backlog满了nginx还向php发请求就会返回503，即拒绝客户端的连接。<br/><br/>502 Bad Gateway（处理超过php的设置时间，并不是执行错误，执行错误是500）<br/>The server was acting as a gateway or proxy and received an invalid response from the upstream server.<br/><br/>503 Service Unavailable（PHP的队列backlog满了还来请求如队列被拒绝的状态码）<br/>The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.<br/><br/>504 Gateway Time-out<br/>The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.<br/><br/>more：https://m.zyku.net/nginx/1753.html<br/><br/>ngx_http_limit_req_module 模块，这个可以限制每秒的请求，超过就返回 503 错误码。<br/> 是用共享内存实现的：http://www.cnblogs.com/fll369/archive/2012/11/26/2789572.html<br/><br/>2012.1.7 更新<br/>编译的时候推荐使用nginx-1.0以上版本，不要加--with-debug参数编译，（感谢agentzh指出）。<br/><br/>hashdos这个事，严格意义上不是各种语言的错了（不过perl的确处理得很好），但是用nginx来擦屁股要干净些。<br/>借鉴tomcat的作法，实现了下面这个nginx-http-hashdos-module，通过设置hashdos（默认on）的开关和body_max_count(默认值1000)，对nginx后面的服务进行安全防护，相比对php或者java进行patch，这或许是最好的办法了。<br/><br/>nginx-http-hashdos-module项目地址<br/>https://github.com/54chen/nginx-http-hashdos-module<br/><br/>如何使用<br/>1.下载zip后保存到一个目录，如~/nginx-http-hashdos-module。<br/>2.cd nginx-1.0.9/<br/>3.重新编译和安装nginx<br/>./configure --prefix=/opt/soft/nginx --with-pcre --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=/path_to/nginx-http-hashdos-module/ &amp;&amp; make &amp;&amp; make install<br/>4.配置打开：<br/>hashdos on;<br/>body_max_count 1000;<br/>location ~ &#92;.php$ &#123;<br/>fastcgi_pass 127.0.0.1:9000;<br/>fastcgi_index index.php;<br/>fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br/>include /opt/soft/nginx/conf/fastcgi_params;<br/>client_body_buffer_size 2m;<br/>client_max_body_size 2m;<br/>&#125;<br/><br/>注意事项<br/>因为计算参数都在内存中进行，所以client_body_buffer_size 与 client_max_body_size 的值一定要相等。推荐2m。<br/><br/>TODO<br/>改进in-file时的post分析。<br/><br/>原理<br/>介于微博上有网友对此文扫一眼之后以为是简单通过client_body_buffer_size和client_max_body_size来做的判断，特加此节。<br/>nginx-reqeust-body在接到请求时，根据header中的声明，判断是保存在内存还是在硬盘中，当大小超过两个buf和client_body_buffer_size大小时，会写入临时文件。<br/>防止hashdos的终极目标是filter用户的输入，所以对用户的输入参数数量进行计数。超过1000（body_max_count的默认数量）时，返回一个413给攻击者。希望不要再渔到各种高级工程师。<br/>参考：http://www.54chen.com/_linux_/nginx-hashdos-help.html<br/>nginx 防止小型ddos攻击<br/><textarea name="code" class="php" rows="15" cols="100">
#!/bin/bash
#我分析的日志为这种格式
#    log_format  main  &#039;$remote_addr - $remote_user [$time_local] $request &#039;
#                  &#039;&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; &#039;
#                 &#039;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#039;;
#
#    access_log  logs/access.log  main;
#
nginx_home=&quot;/usr/local/nginx/&quot;
log_path=&quot;$&#123;nginx_home&#125;logs/&quot;
tail -n10000 $log_path/access.log &#92;
&#124;awk &#039;$7 ~ /&#92;//&#123;print $1,$12&#125;&#039;&#92;
&#124;grep -i -v -E &quot;google&#124;yahoo&#124;baidu&#124;msnbot&#124;FeedSky&#124;sogou&#124;soso&quot;&#92;
&#124;awk &#039;&#123;print $1&#125;&#039;&#92;
&#124;sort&#124;uniq -c&#92;
&#124;sort -rn&#92;
&#124;awk &#039;&#123;if($1&gt;10000) print &quot;deny &quot;$2&quot;;&quot;&#125;&#039;&gt;$nginx_home/conf/denyip.conf
$nginx_home/sbin/nginx -s reload

</textarea><br/><br/><br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] nginx防hashdos模块释出 ，nginx 防止小型ddos攻击 。ngx_http_limit_req_module 模块，这个可以限制每秒的请求，超过就返回 503 错误码。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>