openresty开发系列38--通过Lua+Redis 实现动态封禁IP
一)需求背景
為了封禁某些爬蟲或者惡意用戶對服務器的請求,我們需要建立一個動態的 IP 黑名單。
對于黑名單之內的 IP ,拒絕提供服務。
二)設計方案
實現 IP 黑名單的功能有很多途徑:
1、在操作系統層面,配置 iptables,拒絕指定 IP 的網絡請求;
2、在 Web Server 層面,通過 Nginx 自身的 deny 選項 或者 lua 插件 配置 IP 黑名單;
3、在應用層面,在請求服務之前檢查一遍客戶端 IP 是否在黑名單。
為了方便管理和共享,我們通過 Nginx+Lua+Redis 的架構實現 IP 黑名單的功能
如圖
配置nginx.conf
在http部分,配置本地緩存,來緩存redis中的數據,避免每次都請求redis
lua_shared_dict shared_ip_blacklist 8m; #定義ip_blacklist 本地緩存變量
location /ipblacklist {
?? ?access_by_lua_file /usr/local/lua/access_by_limit_ip.lua;
?? ?echo "ipblacklist";
}
# 編輯 /usr/local/lua/access_by_limit_ip.lualocal function close_redis(red)if not red then returnend --釋放連接(連接池實現) 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.say("set keepalive error : ", err) end endlocal function errlog(...)ngx.log(ngx.ERR, "redis: ", ...) endlocal function duglog(...)ngx.log(ngx.DEBUG, "redis: ", ...) endlocal function getIp()local myIP = ngx.req.get_headers()["X-Real-IP"]if myIP == nil thenmyIP = ngx.req.get_headers()["x_forwarded_for"]endif myIP == nil thenmyIP = ngx.var.remote_addrendreturn myIP; endlocal key = "limit:ip:blacklist" local ip = getIp(); local shared_ip_blacklist = ngx.shared.shared_ip_blacklist--獲得本地緩存的最新刷新時間 local last_update_time = shared_ip_blacklist:get("last_update_time");if last_update_time ~= nil then local dif_time = ngx.now() - last_update_time if dif_time < 60 then --緩存1分鐘,沒有過期if shared_ip_blacklist:get(ip) thenreturn ngx.exit(ngx.HTTP_FORBIDDEN) --直接返回403endreturnend endlocal redis = require "resty.redis" --引入redis模塊 local red = redis:new() --創建一個對象,注意是用冒號調用的--設置超時(毫秒) red:set_timeout(1000) --建立連接 local ip = "10.11.0.215" local port = 6379 local ok, err = red:connect(ip, port) if not ok then close_redis(red)errlog("limit ip cannot connect redis"); elselocal ip_blacklist, err = red:smembers(key);if err thenerrlog("limit ip smembers");else--刷新本地緩存,重新設置 shared_ip_blacklist:flush_all();--同步redis黑名單 到 本地緩存for i,bip in ipairs(ip_blacklist) do--本地緩存redis中的黑名單shared_ip_blacklist:set(bip,true);end--設置本地緩存的最新更新時間shared_ip_blacklist:set("last_update_time",ngx.now());end end if shared_ip_blacklist:get(ip) thenreturn ngx.exit(ngx.HTTP_FORBIDDEN) --直接返回403 end
當redis設置了密碼時代碼如下:
[root@node5 lua]# cat /usr/local/lua/access_by_limit_ip.lua
用戶redis客戶端設置:
添加黑名單IP:
sadd limit:ip:blacklist 10.11.0.148
獲取黑名單IP:
smembers limit:ip:blacklist
10.11.0.215:6379> sadd limit:ip:blacklist 10.11.0.148
10.11.0.215:6379> sadd limit:ip:blacklist 10.11.0.215
10.11.0.215:6379> smembers limit:ip:blacklist
1) "10.11.0.215"
2) "10.11.0.148"
10.11.0.215:6379> smembers limit:ip:blacklist
1) "10.11.0.215"
2) "10.11.0.148"
此方法目前只能實現手動添加黑名單IP進行IP封禁,在某些場景如:半夜如果有人惡意爬取網站服務器可能導致服務器資源耗盡崩潰或者影響業務
下面是改進后的代碼,可以實現自動將訪問頻次過高的IP地址加入黑名單封禁一段時間
nginx.conf配置部分:
location /goodslist {
?? ??? ?set $business "USER";
?? ??? ?access_by_lua_file /usr/local/lua/access_count_limit.lua;
?? ??? ?echo "get goods list success";
?? ?}
lua代碼:
[root@node5 lua]# cat /usr/local/luaaccess_count_limit.lua
# redis的數據
10.11.0.215:6379> get USER-COUNT-10.11.0.148
"16"
10.11.0.215:6379> get USER-BLOCK-10.11.0.148
(nil)
四、總結
以上,便是 Nginx+Lua+Redis 實現的 IP 黑名單功能,具有如下優點:
1、配置簡單、輕量,幾乎對服務器性能不產生影響;
2、多臺服務器可以通過Redis實例共享黑名單;
3、動態配置,可以手工或者通過某種自動化的方式設置 Redis 中的黑名單。
轉載于:https://www.cnblogs.com/reblue520/p/11419918.html
總結
以上是生活随笔為你收集整理的openresty开发系列38--通过Lua+Redis 实现动态封禁IP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openresty开发系列37--ngi
- 下一篇: openresty开发系列39--ngi