nginx+lua+redis自动识别封解禁频繁访问IP

在站点遇到攻击且无明显攻击特征,造成站点访问慢,nginx不断返回502等错误时,可利用nginx+lua+redis实现在指定的时间段内,若单IP的请求量达到指定的数量后对该IP进行封禁,nginx返回403禁止访问。利用redis的expire命令设置封禁IP的过期时间达到在指定的封禁时间后实行自动解封的目的。

一、安装环境:

  • CentOS x64 release 6.4(Final)
  • Nginx-1.4.1
  • Redis 2.6.14
  • LuaJIT-2.0.2
yum install -y gcc gcc-c++ openssl-devel pcre-devel zlib-devel gd-devel GeoIP-devel

二、安装步骤:

1、安装LuaJIT-2.0.2

wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -xzvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make && make install

注:64位系统安装完成后或许还需要将/usr/local/lib/libluajit-5.1.so.2建立软连接到/lib64//libluajit-5.1.so.2,否则在后面nginx启动时会提示找不到依赖库。

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64//libluajit-5.1.so.2

2、安装Redis

wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -xzvf redis-3.2.8.tar.gz
cd redis-3.2.8
make && make install

3、安装Nginx

wget http://nginx.org/download/nginx-1.13.0.tar.gz
tar xf nginx-1.13.0.tar.gz 
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc3.tar.gz
tar xf v0.10.9rc3.tar.gz
wget https://github.com/openresty/redis2-nginx-module/archive/v0.14.tar.gz
tar xf v0.14.tar.gz
cd nginx-1.13.0
./configure --prefix=/app/nginx \
	--user=nginx \
	--group=nginx \
	--with-poll_module \
	--with-http_realip_module \
	--with-http_addition_module \
	--with-http_sub_module \
	--with-http_image_filter_module \
	--with-http_geoip_module \
	--with-http_dav_module \
	--with-http_flv_module \
	--with-http_mp4_module \
	--with-http_gunzip_module \
	--with-http_gzip_static_module \
	--with-http_random_index_module \
	--with-http_secure_link_module \
	--with-http_stub_status_module \
	--add-module=/app/lua-nginx-module-0.10.9rc3 \
	--add-module=/app/redis2-nginx-module-0.14
make
make install

4、下载nginx中lua使用redis需要的依赖包,并将redis.lua到nginx安装目录

wget https://github.com/openresty/lua-resty-redis/archive/v0.26.tar.gz
tar xf v0.26.tar.gz 
cp lua-resty-redis-0.26/lib/resty/redis.lua    /app/nginx

5、在nginx.conf文件的http段引入redis.lua包,加入代码

lua_package_path "/app/nginx/redis.lua;;";

6、编写控制访问lua脚本access.lua放到nginx安装目录的conf目录下

ip_bind_time = 300  --封禁IP时间  
ip_time_out = 60    --指定ip访问频率时间段  
connect_count = 100 --指定ip访问频率计数最大值  
--连接redis  
local redis = require "resty.redis"  
local cache = redis.new()  
local ok , err = cache.connect(cache,"127.0.0.1","6379")  
cache:set_timeout(60000)  
--如果连接失败,跳转到脚本结尾  
if not ok then  
  goto A  
end  
--查询ip是否在封禁段内,若在则返回403错误代码  
--因封禁时间会大于ip记录时间,故此处不对ip时间key和计数key做处理  
is_bind , err = cache:get("bind_"..ngx.var.remote_addr)  
if is_bind == "1" then  
  ngx.exit(403)  
  goto A  
end  
start_time , err = cache:get("time_"..ngx.var.remote_addr)  
ip_count , err = cache:get("count_"..ngx.var.remote_addr)  
--如果ip记录时间大于指定时间间隔或者记录时间或者不存在ip时间key则重置时间key和计数key  
--如果ip时间key小于时间间隔,则ip计数+1,且如果ip计数大于ip频率计数,则设置ip的封禁key为1  
--同时设置封禁key的过期时间为封禁ip的时间  
if start_time == ngx.null or os.time() - start_time > ip_time_out then  
  res , err = cache:set("time_"..ngx.var.remote_addr , os.time())  
  res , err = cache:set("count_"..ngx.var.remote_addr , 1)  
else  
  ip_count = ip_count + 1  
  res , err = cache:incr("count_"..ngx.var.remote_addr)  
  if ip_count >= connect_count then  
    res , err = cache:set("bind_"..ngx.var.remote_addr,1)  
    res , err = cache:expire("bind_",ip_bind_time)  
  end  
end  
--结尾标记  
::A::  
local ok, err = cache:close()  

7、在nginx.conf中需要控制访问的站点location段中加入访问控制代码:

access_by_lua_file /app/nginx/conf/access.lua;

现在可以启动redis和nginx进行测试了。

 

8、方式二 基于Redis的实时IP封禁

某些情况下,需要阻止流氓爬虫的抓取,这可以通过专门的封禁设备去做,但是通过Lua,也可以实现简单版本的封禁。Nginx进程每隔10秒从Redis获取一次最新的禁止IP名单。需要注意的是,如果架构中使用了Haproxy这样类似的负载均衡服务器时, 需要把$remote_addr设置为正确的远端IP地址。这个方法还可以用于HTTP User-Agent字段的检查,要求满足指定条件。

lua_shared_dict banned_ips 1m;

location / {
  access_by_lua '
    local banned_ips = ngx.shared.banned_ips;
    local updated_at = banned_ips:get("updated_at");

    -- only update banned_ips from Redis once every ten seconds:
    if updated_at == nil or updated_at < ( ngx.now() - 10 ) then
      local redis = require "resty.redis";
      local red = redis:new();
      red:set_timeout(200);

      local ok, err = red:connect("your-redis-hostname", 6379);
      if not ok then
        ngx.log(ngx.WARN, "Redis connection error retrieving banned_ips: " .. err);
      else
        local updated_banned_ips, err = red:smembers("banned_ips");
        if err then
          ngx.log(ngx.WARN, "Redis read error retrieving banned_ips: " .. err);
        else
          -- replace the locally stored banned_ips with the updated values:
          banned_ips:flush_all();
          for index, banned_ip in ipairs(updated_banned_ips) do
            banned_ips:set(banned_ip, true);
          end
          banned_ips:set("updated_at", ngx.now());
        end
      end
    end

    if banned_ips:get(ngx.var.remote_addr) then
      ngx.log(ngx.WARN, "Banned IP detected and refused access: " .. ngx.var.remote_addr);
      return ngx.exit(ngx.HTTP_FORBIDDEN);
    end
  ';
}

现在就可以阻止特定IP的访问:

$redis.sadd("banned_ips", "200.1.35.4")

转载于:https://my.oschina.net/mywiki/blog/899189

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值