代碼如下
http{
.............
limit_zone one $binary_remote_addr 10m; //我記得默認(rèn)配置就有,只不過(guò)是注釋掉了,如果沒(méi)有加一下
..............
server{
.................
location {
.........
limit_conn one 20; //連接數(shù)限制
limit_rate 500k; //帶寬限制
........
}
.................
}
.............
}
[root@localhost nginx]# /etc/init.d/nginx reload //重新加載
2,測(cè)試限制ip連接數(shù)
代碼如下
[root@localhost nginx]# webbench -c 100 -t 2
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET
100 clients, running 2 sec.
Speed=429959 pages/min, 2758544 bytes/sec.
Requests: 14332 susceed, 0 failed.
[root@localhost nginx]# cat /var/log/nginx/access.log|grep 503 |more //這樣的數(shù)據(jù)有很多,最好加個(gè)more或者less
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
127.0.0.1 - - [25/Apr/2012:17:52:21 +0800] "GET /index.php HTTP/1.0" 503 213 "-" "WebBench 1.5" -
..............................................................................................
通過(guò)以上測(cè)試,可以得出限制ip連接數(shù)是沒(méi)有問(wèn)題的,但是限制帶寬看不出來(lái),說(shuō)實(shí)話這個(gè)不好測(cè)試,所以就沒(méi)測(cè)試了
利用apache限制IP并發(fā)數(shù)和下載流量控制
安裝mod_limitipconn限制IP連接數(shù)
1,下載地址:
2,安裝:[root@BlackGhost mod_limitipconn-0.22]# /usr/local/apache2/bin/apxs -c -i mod_limitipconn.c
3,配置如下vi httpd.conf
代碼如下
ExtendedStatus On
LoadModule limitipconn_module modules/mod_limitipconn.so
<IfModule mod_limitipconn.c>
<Location /> #對(duì)應(yīng)根目錄
MaxConnPerIP 6 #最大并發(fā)數(shù)
NoIPLimit image/* #對(duì)圖片不做限制
</Location>
<Location /download> #對(duì)根目錄下面的download
MaxConnPerIP 1 #最大并發(fā)數(shù)為1
</Location>
</IfModule>
說(shuō)明:解壓mod_limitipconn-0.22.tar.gz后,文件里面有一個(gè)README里面有配置的拿出來(lái),根據(jù)自己的需要改一改就行了,如果真的不會(huì),可以上網(wǎng)上查,像apache用的人這么多,我想你的問(wèn)題別人也遇到過(guò),一查肯定能查到。如果你想放到虛擬主機(jī)進(jìn)行最大并發(fā)數(shù)控制,可以修改extra/httpd-vhost.conf把<IfModule mod_limitipconn.c>這個(gè)東西copy到<Virtualhost>中就可以了
三,安裝mod_bandwidth
mod_bandwidth可以對(duì)IP的并發(fā)數(shù)進(jìn)行控制,也可以對(duì)下載流量進(jìn)行控制,也可以對(duì)某個(gè)目錄的流量進(jìn)行控制。
1,下載地址:
2,安裝:[root@BlackGhost mod_bw]# /usr/local/apache2/bin/apxs -c -i mod_bw.c
3,配置如下vi httpd.conf 加上LoadModule bw_module modules/mod_bw.so
然后打開(kāi)vi httpd-vhosts.conf