lua+nginx實(shí)現(xiàn)黑名單禁止訪問的示例代碼
可以使用基于 Nginx 與 Lua 的高性能 Web 平臺(tái)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實(shí)現(xiàn)黑名單禁止訪問的示例代碼的文章就介紹到這了,更多相關(guān)lua+nginx黑名單禁止訪問內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx靜態(tài)文件響應(yīng)POST請(qǐng)求 提示405錯(cuò)誤的解決方法
Apache、IIS、nginx等絕大多數(shù)web服務(wù)器,都不允許靜態(tài)文件響應(yīng)POST請(qǐng)求,否則會(huì)返回“HTTP/1.1 405 Method not allowed”錯(cuò)誤2013-04-04
Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法
這篇文章主要介紹了Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Ubuntu+Nginx+Mysql+Php+Zend+eaccelerator安裝配置文字版
把我架設(shè)lnmp網(wǎng)站的過程寫出來,希望對(duì)想架設(shè)網(wǎng)站的朋友有所幫助,如有更好的辦法請(qǐng)?zhí)岢鰜?/div> 2012-02-02
nginx做代理轉(zhuǎn)發(fā)前端請(qǐng)求到后端的代碼示例
Nginx作為反向代理服務(wù)器,可以有效處理請(qǐng)求并轉(zhuǎn)發(fā)到后端服務(wù)器,這篇文章主要介紹了nginx做代理轉(zhuǎn)發(fā)前端請(qǐng)求到后端的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
Nginx實(shí)現(xiàn)https網(wǎng)站配置代碼實(shí)例
這篇文章主要介紹了Nginx實(shí)現(xiàn)https網(wǎng)站配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Nginx 轉(zhuǎn)發(fā)匹配規(guī)則的實(shí)現(xiàn)
這篇文章主要介紹了Nginx 轉(zhuǎn)發(fā)匹配規(guī)則的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03最新評(píng)論

