今天有个人问我,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连接数是没有问题的,但是限制带宽看不出来,说实话这个不好测试,所以就没测试了。
原文链接:https://www.f2er.com/nginx/447353.html