当前位置: 代码网 > it编程>数据库>Redis > lua+nginx实现黑名单禁止访问的示例代码

lua+nginx实现黑名单禁止访问的示例代码

2025年11月05日 Redis 我要评论
可以使用基于 nginx 与 lua 的高性能 web 平台openresty。openresty地址安装简单,略去。 # 分配内存 lua_shared_dict ip_blacklist 1

可以使用基于 nginx 与 lua 的高性能 web 平台openresty。 openresty地址

安装简单,略去。

 # 分配内存
    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) -- 设置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

到此这篇关于lua+nginx实现黑名单禁止访问的示例代码的文章就介绍到这了,更多相关lua+nginx黑名单禁止访问内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com