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=100 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=50 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是锁定时间,单位为秒,这个时间可以自定义,
想锁多久,你开心怎么定都可以,是不是简单易上手呢。经过全面压测,可安心使用

转载于:https://my.oschina.net/MasterXimen/blog/1825545

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值