Nginx 通过 Lua + Redis 实现动态封禁 IP
一 、安装 Openrestry
# 1.下载源码包
wget https://openresty.org/download/openresty-1.11.2.2.tar.gz
tar -xzvf openresty-1.11.2.2.tar.gz
cd openresty-1.11.2.2
# 2.查看 nginx 编译选项:
$ nginx -V
nginx version: nginx/1.10.3
... (省略) ...
# 3. 编译安装
./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_stub_status_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-openssl=/usr/local/src/openssl-1.0.2m
make
make install
二 、 配置 openrestry
# 复制原来的 `nginx` 配置
#openresty-nginx 路径是: /usr/local/openresty/nginx
#将原来的nginx 配置都复制到 /usr/local/openresty/nginx/conf
cp /usr/local/nginx/conf/*.conf /usr/local/openresty/nginx/conf/
## vhost 目录 ssl 目录
cp -r /usr/local/nginx/conf/conf.d /usr/local/openresty/nginx/conf
还要根据服务器实际情况,修改 nginx.conf 等配置文件中替换路径为新的路径
三、 修改环境变量
- 先停止原来的nginx
- 添加环境变:
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
改了之后原来的nginx 就没用了,这里我本来就是替换的,所以就这样设置了。 - 执行 :
source /etc/profile
- 运行
openresty
$ nginx
注意:openresty 在支持http2的时候 响应头header server会修改成nginx
# 验证版本
root@iZm5eabkgmsfy2phj5pr1xZ:~$ nginx -V
nginx version: openresty/1.11.2.2
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
built with OpenSSL 1.0.2m 2 Nov 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 —add-(省略)
四、 使用 lua-redis 实现封禁ip高频访问
1. 查看 access.log 高访问前5条数据
awk '{print $1}' access.log | sort|uniq -c | sort -nr | head -n 5
8 180.169.120.218
1 40.77.167.22
1 207.46.13.192
...
2. 修改 nginx
配置 ,nginx.conf
或者 虚拟主机host
中,添加对 lua
脚本的支持
# 以我自己服务器为例,我的www.leon0204.com 的配置文件在
$ cd /usr/local/openresty/nginx/conf/conf.d/ && vim leon0204.conf
# 添加 openrestry 引入lua脚本要用的地址
lua_package_path "/usr/local/openresty/lualib/resty/redis.lua;";#告诉openresty库地址
#lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
error_log /usr/local/openresty/nginx/logs/openresty.debug.log debug;
# 所有 xxx/hello 的请求会被分发给lua 脚本处理
location /hello {
default_type text/html;
access_by_lua_file "/usr/local/openresty/nginx/lua/access_by_redis.lua";
}
3. lua 的基于访问频率的reids 实现自动化可控封禁脚本
# Lua
local function close_redis(red)
if not red then
return
end
--释放连接(连接池实现)
local pool_max_idle_time = 10000 --毫秒
local pool_size = 100 --连接池大小
local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
if not ok then
ngx_log(ngx_ERR, "set redis keepalive error : ", err)
end
end
-- 连接redis
local redis = require('resty.redis')
local red = redis.new()
red:set_timeout(1000)
local ip = "127.0.0.1" ---修改变量
local port = "6379" ---修改变量
local ok, err = red:connect(ip,port)
if not ok then
return close_redis(red)
end
red:auth('passwd')
--resp = redis_init:set('funet', '888888')
--resp = redis_init:get('funet')
local clientIP = ngx.req.get_headers()["X-Real-IP"]
if clientIP == nil then
clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
if clientIP == nil then
clientIP = ngx.var.remote_addr
end
--ngx.say(clientIP)
--if clientIP == "101.231.137.70" then
-- ngx.exit(ngx.HTTP_FORBIDDEN)
-- return close_redis(red)
-- end
local incrKey = "user:"..clientIP..":freq"
local blockKey = "user:"..clientIP..":block"
local is_block,err = red:get(blockKey) -- check if ip is blocked
--ngx.say(tonumber(is_block))
if tonumber(is_block) == 1 then
--ngx.say(3)
ngx.exit(403)
--ngx.exit(ngx.HTTP_FORBIDDEN)
close_redis(red)
end
inc = red:incr(incrKey)
ngx.say(inc)
if inc < 2 then
inc = red:expire(incrKey,1)
end
if inc > 2 then --每秒2次以上访问即视为非法,会阻止1分钟的访问
red:set(blockKey,1) --设置block 为 True 为1
red:expire(blockKey,60)
end
close_redis(red)
最终效果:
- 在一秒内访问1,2次,nginx 返回 1,2
- 一秒访问3次,直接返回 403 ,参数可控 ,封禁时间可控
分类:
nginx
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)