加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx限制ip连接数和带宽

发布时间:2020-12-13 21:41:58 所属栏目:Nginx 来源:网络整理
导读:今天有个人问我,nginx怎么限制ip连接数,突然想不起来了,年龄大了,脑子不怎么好使了。还要看一下配置才想起了。那个人又问我,你测试过的吗?一下子把我问蒙了,我真没测试过了,也不知道启作用了没有。下面我做了一下测试。以前用apache的时候到是做过测

今天有个人问我,nginx怎么限制ip连接数,突然想不起来了,年龄大了,脑子不怎么好使了。还要看一下配置才想起了。那个人又问我,你测试过的吗?一下子把我问蒙了,我真没测试过了,也不知道启作用了没有。下面我做了一下测试。以前用apache的时候到是做过测试,apache怎么限制ip数,请参考:利用apache限制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连接数是没有问题的,但是限制带宽看不出来,说实话这个不好测试,所以就没测试了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读