在nginx中限制客户端访问频次与访问次数,需要用到二个模块NginxHttpLimitConnModule与NginxHttpLimitReqModule。
nginx的两个模块: NginxHttpLimitReqModule,可以根据设定的条件来限定客户端(单一ip)的访问频率。 下面重点介绍下这两个模块的用法。
1.NginxHttpLimitConnModule
http{
limit_conn_zone $binary_remote_addr zone=one:10m; server { limit_conn test 1; location =/1.html{ root html; } } }
1)limit_conn_zone $binary_remote_addr zone=one:10m;
2)limit_conn test 1;
用ab进行测试
[root@usvr-124 logs]# vim access.log
1 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 200 0 0 235 90 0"-" 2 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 503 0 0 371 90 0"-" 3 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 200 0 0 235 90 0"-" 4 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 503 0 0 371 90 0"-" 5 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 200 0 0 235 90 0"-" 6 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 503 0 0 371 90 0"-" 7 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 503 0 0 371 90 0"-" 8 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 200 0 0 235 90 0"-" 9 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 503 0 0 371 90 0"-" 10 27/Nov/2014:11:25:30 +0800 - - 192.168.3.124:80 GET /1.html - - 192.168.3.126 HTTP/1.0 [ApacheBench/2.3] [-] - www.test.cn 200 0 0 235 90 0"-" 每次访问是两个并发,每两个并发请求中有一个是被拒绝的,返回的是503错误。
看下错误日志:
[root@usvr-124 logs]# cat error/error.log
2014/11/27 11:29:50 [error] 8445#0: *22317249 limiting connections by zone "one", client: 192.168.3.126, server: www.test.cn, request: "GET /1.html HTTP/1.0", host: "www.test.cn" 2014/11/27 11:29:50 [error] 8445#0: *22317251 limiting connections by zone "one", client: 192.168.3.126, server: www.test.cn, request: "GET /1.html HTTP/1.0", host: "www.test.cn" 2014/11/27 11:29:50 [error] 8445#0: *22317255 limiting connections by zone "one", client: 192.168.3.126, server: www.test.cn, request: "GET /1.html HTTP/1.0", host: "www.test.cn" 2014/11/27 11:29:50 [error] 8445#0: *22317257 limiting connections by zone "one", client: 192.168.3.126, server: www.test.cn, request: "GET /1.html HTTP/1.0", host: "www.test.cn" 从上面看出返回503错误的由于被设定的存储区域one所限制,设定的规则生效了。
2.NginxHttpLimitReqModule
http{
limit_req_zone $binary_remote_addr zone=two:10m rate=5r/s; server { limit_req zone=two burst=5 nodelay; location =/1.html{ root html; } } }
1)limit_req_zone $binary_remote_addr zone=two:10m rate=5r/s;
2)limit_req zone=two burst=5 nodelay;
用ab进行测试 ab -c 1 -n 5 -t 2 http://www.leduz.com/1.html,由于在2秒内会请求上前次,因此在这就不把访问日志和错误日志贴出来了,大体描述下:
说明: |