1、在http://dominia.org/djao/limitipconn2.html上下载对应的版本,我下载的是for apache 2.2的版本(mod-limitipconn-0.23.tar.bz2)。
2、安装(可查看INSTALL);
#tar -zxvf mod_limitipconn-0.23.tar.bz2 #cd mod_limitipconn-0.23 #vim Makefile #APXS=/var/www/bin/apxs #make #make install
该步骤会自动将mod_limitipconn.so编译到 apache 对应的modules目录中,还会在httpd.conf文件中自动添加loadmodules语句。
3、修改httpd.conf文件完成配置,For example:
# This command is always needed ExtendedStatus On # Only needed if the module is compiled as a DSO LoadModule limitipconn_module lib/apache/mod_limitipconn.so <IfModule mod_limitipconn.c> # Set a server-wide limit of 10 simultaneous downloads per IP, # no matter what. MaxConnPerIP 10 <Location /somewhere> # This section affects all files under http://your.server/somewhere MaxConnPerIP 3 # exempting images from the connection limit is often a good # idea if your web page has lots of inline images, since these # pages often generate a flurry of concurrent image requests NoIPLimit image/* </Location> <Directory /home/*/public_html> # This section affects all files under /home/*/public_html MaxConnPerIP 1 # In this case, all MIME types other than audio/mpeg and video* # are exempt from the limit check OnlyIPLimit audio/mpeg video </Directory> </IfModule>
在这之中,有如下几点是需要注意的:
a、mod_status一定要加载,且httpd.conf中的ExtendedStatus选项要打开。 b、如果要限制特定目录,请使用如下格式:
<Location /> # global per-directory settings here <Location /somewhere> # local per-directory settings here </Location> </Location>
c、若使用了mod_cache模块,则mod_limitipconn对特定目录的限制无效,只能通过全局参数来限制。(不太理解) d、对连接数的限制是针对所有IP的,不能针对个别IP进行限制。 e、如果有客户端的超过了限制,则会报503错误。 f、 可以将超过连接数的访问记录到access.log中。
相关详情请见http://dominia.org/djao/limitipconn2-README 我的设置如下:
ExtendedStatus On <IfModule limitipconn_module> <Location /> MaxConnPerIP 3 NoIPLimit image/* </Location> <Location /mp3> MaxConnPerIP 1 OnlyIPLimit audio/mpeg video </Location> </IfModule>
4、测试:
刚开始陷入了一个误区,以为可以直接使用ab测试并发数,其实ab是用来测试 apache 的性能。Ab只能反应服务器是否响应了客户端的请求,而不关心服务器是不是拒绝为客户端服务。 测试同一IP的最大连接数的关键是查看apache的access_log,但需要如ab或 多线程 下载工具来配合测试,如我设置的MaxConnPerIP 3,刚我可使用如下命令来测试,ab –n 50 –c 10 127.0.0.1/,查看access_log可看到如下信息:
127.0.0.1 - - [10/Jan/2009:18:43:07 +0800] "GET /index.html HTTP/1.0" 503 323 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 200 44 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 200 44 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 200 44 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 503 323 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 503 323 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 503 323 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 503 323 127.0.0.1 - - [10/Jan/2009:19:01:16 +0800] "GET / HTTP/1.0" 503 323
则说明mod_limitipconn设置成功,服务器对同一IP的最大并发数为3。 由于对ab的机制的不了解,从一个误区跳到了另一个误区,上述测试的结果是不准确的,测试同一IP最大连接数最直接最简单的方法是:放一个较大的文件(如电影)到DocumentRoot中,然后通过迅雷来下载该文件,然后观察apache对迅雷 多线程 的反应,我的迅雷默认有5个线程,但只有3个线程读取数据,与设置相符,修改MaxConnPerIP的值后测试,测试结果也与之相符。MaxConnPerIP的值为0时,表示对连接无限制。
查看更多关于如何使用mod_limitipconn.c来限制apache的并发数_自学的详细内容...