<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog]]></title> 
<link>http://www.bizeway.net/index.php</link> 
<description><![CDATA[网务通 - 网务公司发展之路]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog]]></copyright>
<item>
<link>http://www.bizeway.net/read.php?</link>
<title><![CDATA[查看 Apache并发请求数及其TCP连接状态]]></title> 
<author>ArthurXF &lt;arthurxf@gmail.com&gt;</author>
<category><![CDATA[FreeBSD]]></category>
<pubDate>Tue, 17 Apr 2012 07:46:45 +0000</pubDate> 
<guid>http://www.bizeway.net/read.php?</guid> 
<description>
<![CDATA[ 
	服务器上的一些统计数据：<br/><br/>1)统计80端口连接数<br/>netstat -nat&#124;grep -i "80"&#124;wc -l<br/><br/>2）统计httpd协议连接数<br/>ps -ef&#124;grep httpd&#124;wc -l<br/><br/>3）、统计已连接上的，状态为“established<br/>netstat -na&#124;grep ESTABLISHED&#124;wc -l<br/><br/>4)、查出哪个IP地址连接最多,将其封了.<br/>netstat -na&#124;grep ESTABLISHED&#124;awk {print $5}&#124;awk -F: {print $1}&#124;sort&#124;uniq -c&#124;sort -r +0n<br/><br/>netstat -na&#124;grep SYN&#124;awk {print $5}&#124;awk -F: {print $1}&#124;sort&#124;uniq -c&#124;sort -r +0n<br/><br/>---------------------------------------------------------------------------------------------<br/><br/>1、查看apache当前并发访问数：<br/>netstat -an &#124; grep ESTABLISHED &#124; wc -l<br/><br/>对比httpd.conf中MaxClients的数字差距多少。<br/><br/>2、查看有多少个进程数：<br/>ps aux&#124;grep httpd&#124;wc -l<br/><br/>3、可以使用如下参数查看数据<br/>server-status?auto<br/><br/>#ps -ef&#124;grep httpd&#124;wc -l<br/>1388<br/>统计httpd进程数，连个请求会启动一个进程，使用于Apache服务器。<br/>表示Apache能够处理1388个并发请求，这个值Apache可根据负载情况自动调整。<br/><br/>#netstat -nat&#124;grep -i "80"&#124;wc -l<br/>4341<br/>netstat -an会打印系统当前网络链接状态，而grep -i "80"是用来提取与80端口有关的连接的，wc -l进行连接数统计。<br/>最终返回的数字就是当前所有80端口的请求总数。<br/><br/>#netstat -na&#124;grep ESTABLISHED&#124;wc -l<br/>376<br/>netstat -an会打印系统当前网络链接状态，而grep ESTABLISHED 提取出已建立连接的信息。 然后wc -l统计。<br/>最终返回的数字就是当前所有80端口的已建立连接的总数。<br/><br/>netstat -nat&#124;&#124;grep ESTABLISHED&#124;wc - 可查看所有建立连接的详细记录<br/><br/>查看Apache的并发请求数及其TCP连接状态：<br/>　　Linux命令：<br/>netstat -n &#124; awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'<br/><br/>（这条语句是从 新浪互动社区事业部 新浪互动社区事业部技术总监王老大那儿获得的，非常不错）返回结果示例：<br/>　　LAST_ACK 5<br/>　　SYN_RECV 30<br/>　　ESTABLISHED 1597<br/>　　FIN_WAIT1 51<br/>　　FIN_WAIT2 504<br/>　　TIME_WAIT 1057<br/>　　其中的<br/>SYN_RECV表示正在等待处理的请求数；<br/>ESTABLISHED表示正常数据传输状态；<br/>TIME_WAIT表示处理完毕，等待超时结束的请求数。<br/><br/>---------------------------------------------------------------------------------------------<br/><br/>查看Apache并发请求数及其TCP连接状态<br/><br/>查看httpd进程数（即prefork模式下Apache能够处理的并发请求数）：<br/>　　Linux命令：<br/><br/>ps -ef &#124; grep httpd &#124; wc -l<br/><br/>　　返回结果示例：<br/>　　1388<br/>　　表示Apache能够处理1388个并发请求，这个值Apache可根据负载情况自动调整，我这组服务器中每台的峰值曾达到过2002。<br/> <br/>查看Apache的并发请求数及其TCP连接状态：<br/>　　Linux命令：<br/> <br/>netstat -n &#124; awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'<br/>返回结果示例：<br/>　　LAST_ACK 5<br/>　　SYN_RECV 30<br/>　　ESTABLISHED 1597<br/>　　FIN_WAIT1 51<br/>　　FIN_WAIT2 504<br/>　　TIME_WAIT 1057<br/>　　其中的SYN_RECV表示正在等待处理的请求数；ESTABLISHED表示正常数据传输状态；TIME_WAIT表示处理完毕，等待超时结束的请求数。<br/>　　状态：描述<br/><br/>　　CLOSED：无连接是活动 的或正在进行<br/><br/>　　LISTEN：服务器在等待进入呼叫<br/><br/>　　SYN_RECV：一个连接请求已经到达，等待确认<br/><br/>　　SYN_SENT：应用已经开始，打开一个连接<br/><br/>　　ESTABLISHED：正常数据传输状态<br/><br/>　　FIN_WAIT1：应用说它已经完成<br/><br/>　　FIN_WAIT2：另一边已同意释放<br/><br/>　　ITMED_WAIT：等待所有分组死掉<br/><br/>　　CLOSING：两边同时尝试关闭<br/><br/>　　TIME_WAIT：另一边已初始化一个释放<br/><br/>　　LAST_ACK：等待所有分组死掉<br/><br/> <br/><br/>如发现系统存在大量TIME_WAIT状态的连接，通过调整内核参数解决，<br/>vim /etc/sysctl.conf<br/>编辑文件，加入以下内容：<br/>net.ipv4.tcp_syncookies = 1<br/>net.ipv4.tcp_tw_reuse = 1<br/>net.ipv4.tcp_tw_recycle = 1<br/>net.ipv4.tcp_fin_timeout = 30<br/>然后执行 /sbin/sysctl -p 让参数生效。<br/><br/>net.ipv4.tcp_syncookies = 1 表示开启SYN cookies。当出现SYN等待队列溢出时，启用cookies来处理，可防范少量SYN攻击，默认为0，表示关闭；<br/>net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接，默认为0，表示关闭；<br/>net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收，默认为0，表示关闭。<br/>net.ipv4.tcp_fin_timeout 修改系統默认的 TIMEOUT 时间<br/>下面附上TIME_WAIT状态的意义：<br/><br/>客户端与服务器端建立TCP/IP连接后关闭SOCKET后，服务器端连接的端口<br/>状态为TIME_WAIT<br/><br/>是不是所有执行主动关闭的socket都会进入TIME_WAIT状态呢？<br/>有没有什么情况使主动关闭的socket直接进入CLOSED状态呢？<br/><br/>主动关闭的一方在发送最后一个 ack 后<br/>就会进入 TIME_WAIT 状态 停留2MSL（max segment lifetime）时间<br/>这个是TCP/IP必不可少的，也就是“解决”不了的。<br/><br/>也就是TCP/IP设计者本来是这么设计的<br/>主要有两个原因<br/>1。防止上一次连接中的包，迷路后重新出现，影响新连接<br/>（经过2MSL，上一次连接中所有的重复包都会消失）<br/>2。可靠的关闭TCP连接<br/>在主动关闭方发送的最后一个 ack(fin) ，有可能丢失，这时被动方会重新发<br/>fin, 如果这时主动方处于 CLOSED 状态 ，就会响应 rst 而不是 ack。所以<br/>主动方要处于 TIME_WAIT 状态，而不能是 CLOSED 。<br/><br/>TIME_WAIT 并不会占用很大资源的，除非受到攻击。<br/><br/>还有，如果一方 send 或 recv 超时，就会直接进入 CLOSED 状态<br/><br/>如何合理设置apache httpd的最大连接数？<br/><br/>手头有一个网站在线人数增多，访问时很慢。初步认为是服务器资源不足了，但经反复测试，一旦连接上，不断点击同一个页面上不同的链接，都能迅速打开，这种现象就是说明apache最大连接数已经满了，新的访客只能排队等待有空闲的链接，而如果一旦连接上，在keeyalive 的存活时间内（KeepAliveTimeout，默认5秒）都不用重新打开连接，因此解决的方法就是加大apache的最大连接数。<br/><br/>1.在哪里设置？<br/>apache 2.24，使用默认配置（FreeBSD 默认不加载自定义MPM配置），默认最大连接数是250<br/><br/>在/usr/local/etc/apache22/httpd.conf中加载MPM配置（去掉前面的注释）：<br/># Server-pool management (MPM specific)<br/>Include etc/apache22/extra/httpd-mpm.conf<br/><br/>可见的MPM配置在/usr/local/etc/apache22/extra/httpd-mpm.conf，但里面根据httpd的工作模式分了很多块，哪一部才是当前httpd的工作模式呢？可通过执行 apachectl -l 来查看：<br/>Compiled in modules:<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;core.c<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prefork.c<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http_core.c<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mod_so.c<br/><br/>看到prefork 字眼，因此可见当前httpd应该是工作在prefork模式，prefork模式的默认配置是：<br/><IfModule mpm_prefork_module><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StartServers &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MinSpareServers &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxSpareServers &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxClients &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;150<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxRequestsPerChild &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0<br/></IfModule><br/><br/>2.要加到多少？<br/><br/>连接数理论上当然是支持越大越好，但要在服务器的能力范围内，这跟服务器的CPU、内存、带宽等都有关系。<br/><br/>查看当前的连接数可以用：<br/>ps aux &#124; grep httpd &#124; wc -l<br/><br/>或：<br/>pgrep httpd&#124;wc -l<br/><br/>计算httpd占用内存的平均数:<br/>ps aux&#124;grep -v grep&#124;awk '/httpd/{sum+=$6;n++};END{print sum/n}'<br/><br/>由于基本都是静态页面，CPU消耗很低，每进程占用内存也不算多，大约200K。<br/><br/>服务器内存有2G，除去常规启动的服务大约需要500M（保守估计），还剩1.5G可用，那么理论上可以支持1.5*1024*1024*1024/200000 = 8053.06368<br/><br/>约8K个进程，支持2W人同时访问应该是没有问题的（能保证其中8K的人访问很快，其他的可能需要等待1、2秒才能连上，而一旦连上就会很流畅）<br/><br/>控制最大连接数的MaxClients ，因此可以尝试配置为：<br/><IfModule mpm_prefork_module><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StartServers &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MinSpareServers &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxSpareServers &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ServerLimit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5500<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxClients &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5000<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxRequestsPerChild &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100<br/></IfModule><br/><br/>注意，MaxClients默认最大为250，若要超过这个值就要显式设置ServerLimit，且ServerLimit要放在MaxClients之前，值要不小于MaxClients，不然重启httpd时会有提示。<br/><br/>重启httpd后，通过反复执行pgrep httpd&#124;wc -l 来观察连接数，可以看到连接数在达到MaxClients的设值后不再增加，但此时访问网站也很流畅，那就不用贪心再设置更高的值了，不然以后如果网站访问突增不小心就会耗光服务器内存，可根据以后访问压力趋势及内存的占用变化再逐渐调整，直到找到一个最优的设置值。<br/><br/>(MaxRequestsPerChild不能设置为0，可能会因内存泄露导致服务器崩溃）<br/><br/>更佳最大值计算的公式：<br/><br/>apache_max_process_with_good_perfermance < (total_hardware_memory / apache_memory_per_process ) * 2<br/>apache_max_process = apache_max_process_with_good_perfermance * 1.5<br/><br/>附：<br/><br/>实时检测HTTPD连接数：<br/>watch -n 1 -d "pgrep httpd&#124;wc -l"<br/><br/><br/>Tags - <a href="tag.php?tag=freebsd" rel="tag">freebsd</a> , <a href="tag.php?tag=apache" rel="tag">apache</a>
]]>
</description>
</item><item>
<link>http://www.bizeway.net/read.php?&amp;guid=0#topreply</link>
<title><![CDATA[[评论] 查看 Apache并发请求数及其TCP连接状态]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.bizeway.net/read.php?&amp;guid=0#topreply</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>