首页 > 数据库 >openresty+redis配合 lua脚本封停 IP

openresty+redis配合 lua脚本封停 IP

时间:2024-09-04 12:14:23浏览次数:5  
标签:http -- IP cache redis module lua ip ngx

1.安装openresty-1.21.4.4

tar -xzvf openresty-1.21.4.4.tar.gz
cd openresty-1.21.4.4
mkdir modules

# 到 github中下载ngx_cache_purge-2.3,解压后放到 modules里面
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz


# 编译安装 openresty
./configure --prefix=/usr/local/openresty-1.21.4.4 --with-http_ssl_module --with-debug --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-luajit --with-http_iconv_module --with-stream --with-stream_ssl_module --with-threads --with-http_gunzip_module --with-http_mp4_module --with-http_dav_module --with-http_sub_module --with-http_random_index_module --with-http_flv_module --with-http_auth_request_module --with-http_addition_module --with-http_v2_module --with-pcre  --with-pcre-jit --without-http_redis2_module --with-http_geoip_module --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-http_postgres_module --add-module=/usr/local/download/openresty-1.21.4.4/modules/ngx_cache_purge-2.3

gmake
gmake install

2.安装 redis

自己安装并设置密码

3.lua脚本设置

脚本网上一堆,自己测试下

cd /usr/local/openresty-1.21.4.4
mkdir lua

cat ipblock.lua

ip_bind_time = 30  --封禁IP多长时间
ip_time_out = 10    --指定统计ip访问频率时间范围
connect_count = 10 --指定ip访问频率计数最大值
--上面的意思就是10秒内访问超过10次,自动封 IP 30秒,实际测试下来 6 次就封了。
 
--连接redis
local redis = require "resty.redis"
local cache = redis.new()
local ok , err = cache.connect(cache,"10.1.1.240","6380")
-- redis密码
local res, err = cache:auth("wg1q2w3e")
cache:set_timeout(60000)
 
--如果连接失败,跳转到脚本结尾
if not ok then
  goto Lastend
end
 
--查询ip是否在封禁段内,若在则返回403错误代码
--因封禁时间会大于ip记录时间,故此处不对ip时间key和计数key做处理
is_bind , err = cache:get("bind_"..ngx.var.remote_addr)
 
if is_bind == '1' then
  ngx.exit(ngx.HTTP_FORBIDDEN)
  -- 或者 ngx.exit(403)
  -- 当然,你也可以返回500错误啥的,搞一个500页面,提示,亲您访问太频繁啥的。
  goto Lastend
end
 
start_time , err = cache:get("time_"..ngx.var.remote_addr)
ip_count , err = cache:get("count_"..ngx.var.remote_addr)
 
--如果ip记录时间大于指定时间间隔或者记录时间或者不存在ip时间key则重置时间key和计数key
--如果ip时间key小于时间间隔,则ip计数+1,且如果ip计数大于ip频率计数,则设置ip的封禁key为1
--同时设置封禁key的过期时间为封禁ip的时间
 
if start_time == ngx.null or os.time() - start_time > ip_time_out then
  res , err = cache:set("time_"..ngx.var.remote_addr , os.time())
  res , err = cache:set("count_"..ngx.var.remote_addr , 1)
else
  ip_count = ip_count + 1
  res , err = cache:incr("count_"..ngx.var.remote_addr)
  if ip_count >= connect_count then
    res , err = cache:set("bind_"..ngx.var.remote_addr,1)
    res , err = cache:expire("bind_"..ngx.var.remote_addr,ip_bind_time) --fix keys
  end
end
--结尾标记
::Lastend::
local ok, err = cache:close()

4.openreststy启动

cat localhost.conf 
server {
    listen 80;
    server_name localhost;

    location / {
        root html;
        index index.html index.htm;
        access_by_lua_file "/usr/local/openresty-1.21.4.4/lua/ipblock.lua";
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;          
    }
    access_log /data/logs/nginx/localhost_acc.log;
    error_log /data/logs/nginx/localhost_err.log;
}

5.访问并查看 redis数据

http://IP 统计刷新次数,查看日志

查看 redis

 

标签:http,--,IP,cache,redis,module,lua,ip,ngx
From: https://www.cnblogs.com/yangmeichong/p/18396214

相关文章

  • 前端Pinia教程,Pinia+Vue3+Vite+TypeScript+腾讯IM聊天解决方案项目实战
    前端Pinia教程:‌Pinia+Vue3+Vite+TypeScript+腾讯IM聊天解决方案项目实战在前端开发中,‌随着Vue3的普及和Vite构建工具的兴起,‌结合TypeScript和Pinia进行状态管理成为了一个高效且受欢迎的选择。‌本文将详细介绍如何结合这些技术栈以及腾讯IM聊天解决方案,‌搭建一个高效的前端......
  • IPKISS 绘制 Euler Fixed Bend 的创建方法
    IPKISS绘制EulerFixedBend的创建方法正文正文在IPKISS使用Si_fabPDK创建EulerBend波导结构并导入Lumerical中产生对应仿真结构一文中我们介绍了如何使用IPKISS中的si_fab包创建EulerBend波导。这里我们介绍如何使用si_fab包创建EulerF......
  • 【在 Apipost 8.1.2 版本上定义全局变量】
    场景:`在Apipost8.1.1版本上定义全局变量问题描述1.在Apipost7.2.6版本上正常运行的脚本,同步到Apipost8.1.1版本上执行报错。提示变量未定义:以下是报错的变量。apt.variables.set("token",response.json.access_token);console.log(response.json.access_......
  • pip install 安装时,提示【 Could not install packages due to an OSError: [Errno 13
    参考资料:【Python】已解决:ERROR:CouldnotinstallpackagesduetoanOSError:[WinError5]拒绝访问。我的问题:使用pipinstall安装时,遇到【CouldnotinstallpackagesduetoanOSError:[Errno13]Permissiondenied】的错误,提示可能需要【--user】选项:pipinstall......
  • c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
    1.方法一:在contextMenuStrip1打开时获取控件名称双击contextMenuStrip1在它的opening事件中写入下面的代码:privatevoidcontextMenuStrip1_Opening(objectsender,CancelEventArgse){stringwhichcontrol_name=(senderasContextMenuStrip).So......
  • 前端项目实战Uniapp移动端项目+Vue3+Typescript+AntdVue管理平台
    ‌前端项目实战:‌构建Uniapp移动端项目与Vue3+Typescript+AntdVue管理平台‌在当今的前端开发领域,‌技术的不断迭代和创新为开发者带来了更多的选择和可能性。‌本文将介绍如何使用Uniapp框架开发移动端项目,‌并结合Vue3、‌Typescript以及AntdVue来构建一个高效的管理平台。......
  • 千字复盘:程序员做AI副业、个人IP的进
    大家好,我是程序员X小鹿,前互联网大厂程序员,自由职业2年+,也一名AIGC爱好者,持续分享更多前沿的「AI工具」和「AI副业玩法」,欢迎一起交流~最近一直在外面玩,所以更新地不是很勤。上周末去北京参加了AI破局合伙人线下大会,写了一下复盘。有些问题,可能一些在做个人IP、做副业的同学也正......
  • 处理 multipart/form-data
    multipart/form-dataContentType,专门用于处理包含二进制数据(如图片、视频或文档)和常规文本数据的表单,通常用来上传文件。要处理multipart/form-data请求,我们必须用@MultipartConfig或在web.xml中配置Servlet。@MultipartConfig提供了各种参数来控制文件上传行为,如loc......
  • Redis持久化
    Redis的数据都是存储在内存中,为了数据的永久保存,需要把数据同步到硬盘上,这个过程就叫做持久化.Redis的持久化存在有两种方式:rdb方式,aof方式,这两种方式可以单独使用,也可以综合使用.1.RDB(RedisDatabaseBackupfile(Redis数据备份文件))RDB也被叫做Redis数据快照(snapshot......
  • 0 JavaScript高级程序设计(第4版)【JS红宝书】【详细思维导图】【持续更新】
    ProcessOn访问链接JavaScript高级程序设计(第4版)阅读路线图,涵盖:基本知识进阶内容BOM和DOMJavascriptAPIJavaScript设计模式和实践策略ProcessOn访问链接......