国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

nginx ip黑名單動態(tài)封禁的例子

2024-08-30 12:24:16
字體:
供稿:網(wǎng)友

網(wǎng)站被惡意請求,拉黑IP是重要的手段,如果每次拉黑都要到nginx上配置,未免太low了;我們需要更方便的控制nginx IP黑名單。

1.方案

黑名單持久化到mysql (常見的方案是redis,但不利于控制,如:不同的IP設(shè)置不同的有效期、IP的CRUD、統(tǒng)計等等);

通過lua-nginx-module,在nginx中開辟一塊內(nèi)存(lua_shared_dict),lua將黑名單定期從mysql全量刷新至lua_shared_dict;

所有請求,都要到與lua_shared_dict中的IP check一下。

2.安裝

2.1 安裝luajit

cd LuaJIT-2.0.5makemake install PREFIX=/usr/local/luajit

2.2.安裝nginx時,將lua模塊編譯進去

export LUAJIT_LIB=/usr/local/luajit/libexport LUAJIT_INC=/usr/local/luajit/include/luajit-2.1 ./configure --prefix=/nginx /--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" /--add-module=/opt/ngx_devel_kit-0.3.1rc1 /--add-module=/opt/lua-nginx-module-0.10.14rc3 make -j2make installln -s /nginx/sbin/nginx /usr/sbin/nginx

3.配置

3.1 nginx配置

http {  server_tokens off;  lua_package_path "/usr/local/lib/lua/?.lua;;";  lua_shared_dict ip_blacklist 4m;} server {  set $real_ip $remote_addr;  if ( $http_x_forwarded_for ~ "^(/d+/./d+/./d+/./d+)" ) {    set $real_ip $1;  }   # 管理信息,訪問該URL可以查看nginx中的IP黑名單信息  location /get-ipblacklist-info {    access_by_lua_file conf/lua/get_ipblacklist_info.lua;  }   # 同步URL,通過定時任務(wù)調(diào)用該URL,實現(xiàn)IP黑名單從mysql到nginx的定時刷新  location /sync-ipblacklist {   access_by_lua_file conf/lua/sync_ipblacklist.lua;  }   # 生產(chǎn)域名配置,所有需要IP黑名單控制的location,都要包含以下語句  location / {   access_by_lua_file conf/lua/check_realip.lua;  } }

nginx服務(wù)器配置以下crrontab

* * * * * /usr/bin/curl -o /dev/null -s http://127.0.0.1/sync-ipblacklist > /dev/null 2>&1

3.2 lua腳本

sync_ipblacklist.lua

local mysql_host = "ip of mysql server"local mysql_port = 3306local database = "dbname"local username = "user"local password = "password" -- update ip_blacklist from mysql once every cache_ttl secondslocal cache_ttl  = 1local mysql_connection_timeout = 1000 local client_ip  = ngx.var.real_iplocal ip_blacklist  = ngx.shared.ip_blacklistlocal last_update_time = ip_blacklist:get("last_update_time"); if last_update_time == nil or last_update_time < ( ngx.now() - cache_ttl ) then  local mysql = require "resty.mysql"; local red = mysql:new();  red:set_timeout(mysql_connect_timeout);  local ok, err, errcode, sqlstate = red:connect{     host = mysql_host,     port = mysql_port,     database = database,     user = username,     password = password,     charset = "utf8",     max_packet_size = 1024 * 1024,    } if not ok then ngx.log(ngx.ERR, "mysql connection error while retrieving ip_blacklist: " .. err); else new_ip_blacklist, err, errcode, sqlstate = red:query("select ip_addr from ip_blacklist where status = 0 order by create_time desc limit 10000", 100) if not new_ip_blacklist then  ngx.log(ngx.ERR, "bad result. errcode: " .. errcode .. " sqlstate: " .. sqlstate .. " err: " .. err);  return end  ip_blacklist:flush_all(); for k1, v1 in pairs(new_ip_blacklist) do  for k2, v2 in pairs(v1) do  ip_blacklist:set(v2,true);  end end  ip_blacklist:set("last_update_time", ngx.now()); endend ngx.say("sync successful");            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石棉县| 千阳县| 岳池县| 舞钢市| 黄平县| 乡城县| 西充县| 兴安县| 凤山市| 南昌县| 五家渠市| 天柱县| 武川县| 千阳县| 阳城县| 阳江市| 宾川县| 香河县| 万年县| 南汇区| 兴和县| 象山县| 建始县| 高要市| 湘阴县| 全州县| 莱西市| 眉山市| 邳州市| 三穗县| 长兴县| 三穗县| 大安市| 镇沅| 惠州市| 双江| 聂荣县| 元氏县| 和林格尔县| 积石山| 宜兰市|