nginx ngx_dynamic_limit_req_module进行动态限流防刷接口


声明:本文转载自https://my.oschina.net/MasterXimen/blog/1825545,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

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

本文发表于2018年06月07日 08:00
(c)注:本文转载自https://my.oschina.net/MasterXimen/blog/1825545,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2667 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1