lua+nginx實現(xiàn)黑名單禁止訪問的示例代碼
更新時間:2025年11月05日 09:42:26 作者:蚊子愛喝水
本文介紹如何使用Nginx與Lua腳本通過OpenResty平臺實現(xiàn)高效的IP黑名單功能,利用Redis存儲黑名單,定時更新共享內(nèi)存字典,防止惡意IP訪問,感興趣的可以了解一下
可以使用基于 Nginx 與 Lua 的高性能 Web 平臺OpenResty。 OpenResty地址
安裝簡單,略去。
# 分配內(nèi)存
lua_shared_dict ip_blacklist 1m;
server {
listen 80;
server_name localhost;
root E:/www/web/test;
access_log logs/host.access.log ;
error_log logs/host.error.log;
location / {
access_by_lua_file ../lua/black.lua;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
# 指定lua文件
access_by_lua_file "D:\openresty-1.15.8.1-win64/lua/black.lua";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}black.lua
local redis_host = "127.0.0.1" -- 這里一定是redis的IP地址
local redis_port = "6379"
-- connection timeout for redis in ms. don't set this too high!
local redis_connection_timeout = 1000
-- check a set with this key for blacklist entries
local redis_key = "ip_blacklist"
-- cache lookups for this many seconds
local cache_ttl = 100
-- end configuration
local ip = ngx.var.remote_addr
local ip_blacklist = ngx.shared.ip_blacklist
local last_update_time = ip_blacklist:get("last_update_time");
-- only update ip_blacklist from Redis once every cache_ttl seconds:
if last_update_time == nil or last_update_time < ( ngx.now() - cache_ttl ) then
local redis = require "resty.redis";
local red = redis:new();
red:set_timeout(redis_connect_timeout);
local ok, err = red:connect(redis_host, redis_port);
if not ok then
ngx.say("redis connect failed: ", err)
ngx.log(ngx.DEBUG, "Redis connection error while retrieving ip_blacklist: " .. err);
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
else
-- local res, err = red:auth("foobared") -- 配置redis的密碼
--if not res then
--ngx.say("redis auth is error: ", err)
--return
--end
red:select(0) -- 設(shè)置redis的db
local new_ip_blacklist, err = red:smembers(redis_key);
if err then
ngx.log(ngx.DEBUG, "Redis read error while retrieving ip_blacklist: " .. err);
else
-- replace the locally stored ip_blacklist with the updated values:
ip_blacklist:flush_all();
for index, banned_ip in ipairs(new_ip_blacklist) do
ip_blacklist:set(banned_ip, true);
end
-- update time
ip_blacklist:set("last_update_time", ngx.now());
end
end
end
if ip_blacklist:get(ip) then
--ngx.say(ip)
ngx.log(ngx.DEBUG, "Banned IP detected and refused access: " .. ip);
return ngx.exit(ngx.HTTP_FORBIDDEN);
end到此這篇關(guān)于lua+nginx實現(xiàn)黑名單禁止訪問的示例代碼的文章就介紹到這了,更多相關(guān)lua+nginx黑名單禁止訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
nginx反向代理服務(wù)器及負載均衡服務(wù)配置方法
正向代理一般是在客戶端設(shè)置代理服務(wù)器,通過代理服務(wù)器轉(zhuǎn)發(fā)請求,最終訪問到目標服務(wù)器,這篇文章主要介紹了nginx反向代理服務(wù)器及負載均衡服務(wù)配置方法,需要的朋友可以參考下2023-12-12
實現(xiàn)Nginx中使用PHP-FPM時記錄PHP錯誤日志的配置方法
最近在本地搭建的LNMP的開發(fā)環(huán)境。為了開發(fā)的時候不影響前端的正常開發(fā)就屏蔽的PHP里面php.ini中的一些錯誤提示。但是這樣一來,就影響到了后端開發(fā)的一些問題比如不能及時調(diào)試開發(fā)中的一些問題2014-05-05
Nginx部署https網(wǎng)站并配置地址重寫的步驟詳解
今天小編就為大家分享一篇關(guān)于Nginx部署https網(wǎng)站并配置地址重寫的步驟詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03

