当前位置:网站首页>openresty 动态黑白名单
openresty 动态黑白名单
2022-08-01 19:16:00 【o_瓜田李下_o】
openresty 动态黑白名单
动态黑白名单
客户端请求发送到openresty,openresty需要验证黑白名单;
可将黑白名单存放在redis中,openresty直接与redis交互,验证黑白名单;
openresty也可将黑白名单加载到共享内存,在内存中验证黑白名单;
验证通过后,执行后续请求;
验证不通过,向客户端返回403(request forbidden)错误
使用示例
创建redis 容器
docker run -it -d --net fixed --ip 172.18.0.81 --name redis-black redis
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /test {
access_by_lua_block {
local redis = require 'resty.redis';
local red = redis:new();
red:set_timeouts(1000, 1000, 1000);
local ok, err = red:connect("172.18.0.81", 6379);
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err)
return
end
local remote_addr = ngx.var.remote_addr;
ngx.log(ngx.ERR, "remote_addr ==> ", remote_addr);
ngx.log(ngx.ERR, "red:sismember('balck-list', remote_addr)");
res, err = red:sismember('black-list', remote_addr);
ngx.log(ngx.ERR, "是否在黑名单 ==> ", res);
if res == 1 then
ngx.log(ngx.ERR, "输出 403");
ngx.exit(ngx.HTTP_FORBIDDEN);
else
ngx.log(ngx.ERR, "输出 200");
ngx.exit(ngx.OK);
end
}
echo "test";
content_by_lua_block {
ngx.say("test");
}
}
location /add {
content_by_lua_block {
local redis = require 'resty.redis';
local red = redis:new();
red:set_timeouts(1000, 1000, 1000);
local ok, err = red:connect("172.18.0.81", 6379);
if not ok then
ngx.say("failed to connect: ", err)
return
end
local members = red:smembers("black-list");
if members then
ngx.say("添加前黑名单 ==> ", members);
end
local ip = ngx.var.arg_ip;
ngx.say("red:sadd('balck-list', ip)");
local res, err = red:sadd('black-list', ip);
if not res then
ngx.say("failed to sadd: ", err)
return
end
local members = red:smembers("black-list");
if members then
ngx.say("添加后黑名单 ==> ", members);
end
}
}
location /delete {
content_by_lua_block {
local redis = require 'resty.redis';
local red = redis:new();
red:set_timeouts(1000, 1000, 1000);
local ok, err = red:connect("172.18.0.81", 6379);
if not ok then
ngx.say("failed to connect: ", err)
return
end
local members = red:smembers("black-list");
if members then
ngx.say("删除前黑名单 ==> ", members);
end
local ip = ngx.var.arg_ip;
ngx.say("red:srem('balck-list', ip)");
local res, err = red:srem('black-list', ip);
if not res then
ngx.say("failed to srem: ", err)
return
end
local members = red:smembers("black-list");
if members then
ngx.say("删除后黑名单 ==> ", members);
end
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
创建openresty 容器
docker run -it -d --net fixed --ip 172.18.0.82 -p 6001:80 \
-v /Users/huli/lua/openresty/black/default.conf:/etc/nginx/conf.d/default.conf \
--name open-black lihu12344/openresty
使用测试
# 初始黑名单为空
[email protected] black % curl localhost:6001/test
test
# 添加黑名单
[email protected] black % curl --location --request GET 'localhost:6001/add?ip=172.18.0.1'
添加前黑名单 ==> gtlx
red:sadd('balck-list', ip)
添加后黑名单 ==> 172.18.0.1gtlx
[email protected] black % curl localhost:6001/test
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>openresty/1.21.4.1</center>
</body>
</html>
# 删除黑名单
[email protected] black % curl --location --request GET 'localhost:6001/delete?ip=172.18.0.1'
删除前黑名单 ==> 172.18.0.1gtlx
red:srem('balck-list', ip)
删除后黑名单 ==> gtlx
[email protected] black % curl localhost:6001/test
test
边栏推荐
- MLX90640 Infrared Thermal Imager Temperature Measurement Module Development Notes (Complete)
- MySQL database - stored procedures and functions
- 【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
- Map by value
- 力扣刷题之求两数之和
- 【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)
- 1个小时!从零制作一个! AI图片识别WEB应用!
- kubernetes-部署nfs存储类
- LeetCode 1374. Generate an odd number of each character string
- What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?
猜你喜欢
经验共享|在线文档协作:企业文档处理的最佳选择
LeetCode 0152. Product Maximum Subarray: dp + Roll in Place
Library website construction source code sharing
Hardware Bear Original Collection (Updated 2022/07)
How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
Win11如何开启剪贴板自动复制?Win11开启剪贴板自动复制的方法
力扣刷题之求两数之和
屏:全贴合工艺之GFF、OGS、Oncell、Incell
硬件大熊原创合集(2022/07更新)
BN BatchNorm + BatchNorm的替代新方法KNConvNets
随机推荐
通配符 SSL/TLS 证书
Risc-v Process Attack
The XML configuration
重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作?
Find the sum of two numbers
Library website construction source code sharing
modbus总线模块DAM-8082
开源视界 | StreamNative 盛宇帆:和浪漫的人一起做最浪漫的事
Become a Contributor in 30 minutes | How to participate in OpenHarmony's open source contributions in multiple ways?
明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度
Win11如何删除升级包?Win11删除升级包的方法
Combining two ordered arrays
Redis的内存淘汰策略和过期删除策略的区别是什么
How to query database configuration parameters in GBase 8c, such as datestyle.What function or syntax to use?
MLX90640 Infrared Thermal Imager Temperature Measurement Module Development Notes (Complete)
为你的“架构”安排定期体检吧!
LeetCode 1374. Generate an odd number of each character string
百度无人驾驶商业化已“上路”
ExcelPatternTool: Excel form-database mutual import tool