Nginx

1个成员

nginx与apache 限制ip连接数和带宽方法

发表于 2017-01-30 3688 次查看
今天有个人问我,nginx怎么限制ip连接数,突然想不起来了,年龄大了,脑子不怎么好使了。还要看一下配置才想起了。那个人又问我,你测试过的吗?一下子把我问蒙了,我真没测试过了,也不知道启作用了没有。

1,配置nginx.conf

 代码如下 复制代码

http{
.............
limit_zone   one  $binary_remote_addr  10m;  //我记得默认配置就有,只不过是注释掉了,如果没有加一下
..............
 server{
 .................
 location {
 .........
 limit_conn one 20;          //连接数限制
 limit_rate 500k;            //带宽限制
 ........
 }
 .................
 }
.............
}

[root@localhost nginx]# /etc/init.d/nginx reload //重新加载

2,测试限制ip连接数

 代码如下 复制代码

[root@localhost nginx]#  webbench -c 100 -t 2 http://127.0.0.1/index.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://127.0.0.1/index.php
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   //这样的数据有很多,最好加个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" -

..............................................................................................

通过以上测试,可以得出限制ip连接数是没有问题的,但是限制带宽看不出来,说实话这个不好测试,所以就没测试了


利用apache限制IP并发数和下载流量控制


安装mod_limitipconn限制IP连接数

1,下载地址:http://dominia.org/djao/limitipconn2.html

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 />                                              #对应根目录  
         MaxConnPerIP 6                                  #最大并发数  
         NoIPLimit image/*                              #对图片不做限制  
    </Location>  
    <Location /download>                             #对根目录下面的download  
         MaxConnPerIP 1                                    #最大并发数为1  
    </Location>  
</IfModule>

说明:解压mod_limitipconn-0.22.tar.gz后,文件里面有一个README里面有配置的拿出来,根据自己的需要改一改就行了,如果真的不会,可以上网上查,像apache用的人这么多,我想你的问题别人也遇到过,一查肯定能查到。如果你想放到虚拟主机进行最大并发数控制,可以修改extra/httpd-vhost.conf把<IfModule mod_limitipconn.c>这个东西copy到<Virtualhost>中就可以了

三,安装mod_bandwidth

mod_bandwidth可以对IP的并发数进行控制,也可以对下载流量进行控制,也可以对某个目录的流量进行控制。

1,下载地址:http://bwmod.sourceforge.net/

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
然后打开vi httpd-vhosts.conf

 代码如下 复制代码
listen 10004
NameVirtualHost *:10004
<VirtualHost *:10004>
 DocumentRoot "/home/zhangy/www/test"
 ServerName *:10004
 BandwidthModule On
 ForceBandWidthModule On
 Bandwidth all 1024000
 MinBandwidth all 50000
 LargeFileLimit * 500 50000
 MaxConnection all 6
 ErrorLog "/home/zhangy/apache/www.test.com-error.log"
 CustomLog "/home/zhangy/apache/www.test.com-error.log" common
</VirtualHost>

解压bandwidth的压缩文件后,里面有一个mod_bw.txt有详细的说明和实例,下面是部分参数说明:

1,BandWidth localhost 0                                      #对localhost不限速
2,BandWidth 192.168.1.5 102400                       #对192.168.1.5限速为100KB

3,BandWidth “u:^Mozilla(.*)” 10240              #用mozilla时限速10KB
4,BandWidth “u:wget” 102400                      #如果用wget下载时限速10KB

5,MinBandWidth all -1                                      #保证每个客户端最高速度可达10KB
6,LargeFileLimit .jpg 100 10240                      #jpg文件超过100KB,限速10KB

7,#下面的510挺好,如果不设置,apache自己会报错,就根报404差不多,页面非常的丑
ErrorDocument 510 /exceed_speed.html
BandWidthError 510

8,MaxConnection all 10                                       #所有ip最大连接数为10
9,MaxConnection 192.168.1.5 5                         #192.168.1.5最大连接数为5

发表回复
你还没有登录,请先登录注册