当前位置: 首页 > news >正文

Ubuntu 22.04.4 LTS openresty(Nginx) 通过Lua+Redis 实现动态封禁IP

1 系统环境

test@iZbp1g7fmjea77vsqc5hmmZ:~$ cat  /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
#
test@iZbp1g7fmjea77vsqc5hmmZ:~$ /usr/local/openresty/nginx/sbin/nginx  -v
nginx version: openresty/1.25.3.1
#
test@iZbp1g7fmjea77vsqc5hmmZ:~$ redis-cli  -v
redis-cli 7.3.240

2 修改nginx配置

#更加实际情况修改相关配置文件server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;access_by_lua_file "/usr/local/openresty/nginx/lua/access_limit.lua";#封禁lua文件}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}

3  lua配置文件

sudo vim  /usr/local/openresty/nginx/lua/access_limit.lua
package.path = '/usr/local/openresty/lualib/resty/?.lua;'
function get_client()local redis = require "redis"client = redis:new()client:set_timeout(300000) -- 1 secondclient:set_keepalive(600000, 200)local ip = '172.16.195.13'local port = 6379local redis_db = 11local ok, err = client:connect(ip,port)if not ok thenreturn nil, "failed to connect."endlocal ok1,err = client:auth("password")if not ok1 thenreturn nil, "failed to authenticate."endclient:select(redis_db)return client, nil
endfunction auth(client, sid)key = ":1:"..sidlocal res, err = client:get(key)return res, nil
end
local function getIp()local clientIP = ngx.req.get_headers()["X-Real-IP"]if clientIP == nil thenclientIP = ngx.req.get_headers()["x_forwarded_for"]endif clientIP == nil thenclientIP = ngx.var.remote_addrendreturn clientIP
end
local clientip = getIp()
local incrKey = "ipcount:"..clientip
local blockKey = "iplimit:"..clientip
client, err = get_client()
if err thenngx.log(ngx.ERR, "connect redis error:", err)ngx.status = 500--ngx.say("{\"detail\":\"server error\"}")ngx.say("{\"detail\":\"redis sid no !\"}")return
end
local is_block,err = client:get(blockKey)
if tonumber(is_block) == 1 thenngx.exit(403)--ngx.exit(ngx.HTTP_FORBIDDEN)client:close()
end
local ip_count, err = client:incr(incrKey)
if  tonumber(ip_count) == 1 thenclient:expire(incrKey,1)
end
if  tonumber(ip_count) > 9 thenclient:set(blockKey,1)client:expire(blockKey,7200) -- 封锁IP 2小时
end
client:close()

4 测试,正常访问不超过9次/秒

#如果每秒访问超过9次,如下

#查看redis

 

5 Redis incr 命令

#存储在指定key中的数值进行自增操作。 当执行INCR命令时,如果key不存在,Redis会创建一个新的key,并将其初始值设置为0,再进行自增操作。如果key的值可以被解释为一个整数,Redis会将其自增1,并返回自增后的值.
127.0.0.1:6379[11]> incr  hihi

相关文章:

  • 中介子方程二十八
  • Talking-Heads Attention
  • Kotlin 中的 infix 关键字(中缀函数)
  • C# 集合(二) —— List/Queue类
  • 马斯克的Grok-1:开源AI模型的突破与挑战
  • TrueNAS系统在ARM平台上的移植
  • 傅佩荣教授讲座视频全集,傅佩荣讲座大全,傅佩荣国学讲座全集百度网盘
  • 使用同步和异步方式更新插入MongoDB数据的性能对比
  • 使用Scala爬取安居客房产信息并存入CSV文件
  • AI时代:硬件狂欢,软件落寞 华为开发者大会2024
  • 如何在 MySQL 中创建和使用事务?
  • 一文读懂数据仓库ODS层
  • 外贸SEO工具有哪些推荐?
  • Unity URP下通过相机让部分Render不受后处理渲染
  • 前端模糊搜索关键字高亮
  • 〔开发系列〕一次关于小程序开发的深度总结
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Consul Config 使用Git做版本控制的实现
  • iOS 系统授权开发
  • JAVA 学习IO流
  • Java|序列化异常StreamCorruptedException的解决方法
  • javascript 哈希表
  • JavaScript类型识别
  • Java新版本的开发已正式进入轨道,版本号18.3
  • Laravel Telescope:优雅的应用调试工具
  • leetcode46 Permutation 排列组合
  • PAT A1050
  • React Transition Group -- Transition 组件
  • Redis字符串类型内部编码剖析
  • vue-router的history模式发布配置
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 使用 @font-face
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 小李飞刀:SQL题目刷起来!
  • 怎样选择前端框架
  • ​1:1公有云能力整体输出,腾讯云“七剑”下云端
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (一)Linux+Windows下安装ffmpeg
  • (一)插入排序
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • *算法训练(leetcode)第三十九天 | 115. 不同的子序列、583. 两个字符串的删除操作、72. 编辑距离
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .Net CF下精确的计时器
  • .NET 动态调用WebService + WSE + UsernameToken
  • .net和php怎么连接,php和apache之间如何连接
  • /etc/fstab 只读无法修改的解决办法
  • /etc/shadow字段详解
  • /var/spool/postfix/maildrop 下有大量文件
  • @SuppressWarnings(unchecked)代码的作用
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)