当前位置:网站首页>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
边栏推荐
- 工作5年,测试用例都设计不好?来看看大神的用例设计总结
- COS User Practice Call for Papers
- How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
- kubernetes - deploy nfs storage class
- Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法
- LeetCode 0151. Reverse a string of words
- Try compiling QT test on Allwinner V853 development board
- Map传值
- 明日盛会|ApacheCon Asia 2022 Pulsar 技术议题一览
- Win11如何删除升级包?Win11删除升级包的方法
猜你喜欢

文库网站建设源码分享

From ordinary advanced to excellent test/development programmer, all the way through

【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)

odoo 编码规范(编程规范、编码指南)

MLX90640 红外热成像仪测温模块开发笔记(完整篇)

明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度
![[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)](/img/8d/4f5f7c2463b781cba1c68370d3c29c.png)
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)

Risc-v Process Attack

How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
如何看待腾讯云数据库负责人林晓斌借了一个亿炒股?
随机推荐
The solution to the vtk volume rendering code error (the code can run in vtk7, 8, 9), and the VTK dataset website
日志工厂(详细)
【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)
从普通进阶成优秀的测试/开发程序员,一路过关斩将
Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
选择合适的 DevOps 工具,从理解 DevOps 开始
数值矩阵的图形表示
Database Plus 的云上之旅:SphereEx 正式开源 ShardingSphere on Cloud 解决方案
Clip-on multimeter use method, how to measure the voltage, current, resistance?
Risc-v Process Attack
手撸代码,Redis发布订阅机制实现
Ha ha!A print function, quite good at playing!
Prometheus的Recording rules实践
ThreadLocal讲义
Mobile Zero of Likou Brush Questions
#yyds干货盘点# 面试必刷TOP101: 链表中倒数最后k个结点
Find the sum of two numbers
网站建设流程
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
【服务器数据恢复】服务器Raid5阵列mdisk组中多块磁盘离线的数据恢复案例