nginx ngx_dynamic_limit_req_module进行动态限流

关于动态限流目前有lua+redis实现方案,但是呢编译lua模块麻烦且一堆东西要依赖,还得自己实现lua脚本,对于很多小白同学,是很难的,所以今天介绍下更加简单的方案,当然也需要一个模块就是ngx_dynamic_limit_req_module 这是在ngx_limit_req_module 基础上做的扩展加上redis定时的功能

https://github.com/limithit/ngx_dynamic_limit_req_module

安装步骤请移步至github或者码云

https://gitee.com/MasterXimen/ngx_dynamic_limit_req_module

因为我真的很懒不想打字哈哈,还请见谅

具体配置如下:


The ngx_dynamic_limit_req_module is used to dynamic lock IP and release regularly.

Configuration example:

worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    dynamic_limit_req_zone $binary_remote_addr zone=one:10m rate=100r/s redis=127.0.0.1 block_second=300;
    dynamic_limit_req_zone $binary_remote_addr zone=two:10m rate=50r/s redis=127.0.0.1 block_second=600;
    
    
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            dynamic_limit_req zone=one burst=5 nodelay;
            dynamic_limit_req_status 403;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  localhost2;
        location / {
            root   html;
            index  index.html index.htm;
            dynamic_limit_req zone=two burst=5 nodelay;
            dynamic_limit_req_status 403;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

支持黑白名单

White list rules

redis-cli set whiteip ip

如: redis-cli set white192.168.1.1 192.168.1.1

Black list rules

redis-cli set ip ip

如: redis-cli set 192.168.1.2 192.168.1.2

历史记录也有保存

dynamic_limit_req zone=two burst=5 nodelay;
dynamic_limit_req_status 403;

这俩参数也可以location 中的if 写入什么意思呢?比如:
   location / {
            root   html;
            index  index.html index.htm;
             
          if ($document_uri ~* "index.php"){          
            dynamic_limit_req zone=two burst=5 nodelay;
            dynamic_limit_req_status 403;
                 }

            
        }
可以对具体的接口或者页面进行限流,而不用全局限制,block_second是锁定时间,单位为秒,这个时间可以自定义,
想锁多久,你开心怎么定都可以,是不是简单易上手呢。经过全面压测,可安心使用

猜你喜欢

转载自my.oschina.net/MasterXimen/blog/1825545
今日推荐