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

二、安装步骤:

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启动时会提示找不到依赖库。

2、安装Redis 2.6.14

wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
tar -xzvf redis-2.6.14.tar.gz
cd redis-2.6.14
make && make install

3、安装Nginx-1.4.1

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz
wget https://codeload.github.com/agentzh/echo-nginx-module/tar.gz/v0.45
wget https://codeload.github.com/chaoslawful/lua-nginx-module/tar.gz/v0.8.3
wget https://codeload.github.com/agentzh/redis2-nginx-module/tar.gz/v0.10
wget http://nginx.org/download/nginx-1.4.1.tar.gz
tar -xzvf pcre-8.21.tar.gz
tar -xzvf openssl-1.0.1e.tar.gz
tar -xzvf echo-nginx-module-0.45.tar.gz
tar -xzvf lua-nginx-module-0.8.3.tar.gz
tar -xzvf redis2-nginx-module-0.10.tar.gz
tar -xzvf nginx-1.4.1.tar.gz
cd nginx-1.4.1./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/nginx \
--user=www \
--group=www \
--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 \
--with-pcre=/home/pcre-8.21 \
--with-openssl=/home/openssl-1.0.1e \
--add-module=/home /echo-nginx-module-0.45 \
--add-module=/home /lua-nginx-module-0.8.3 \
--add-module=/home /redis2-nginx-module-0.10
make
make install

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

https://codeload.github.com/agentzh/lua-resty-redis/tar.gz/v0.15

5、将控制访问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)--如果连接失败,跳转到脚本结尾ifnot ok thengoto A  
end--查询ip是否在封禁段内,若在则返回403错误代码--因封禁时间会大于ip记录时间,故此处不对ip时间key和计数key做处理  
is_bind , err = cache:get("bind_"..ngx.var.remote_addr)if is_bind ==1then  
  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的封禁key1--同时设置封禁key的过期时间为封禁ip的时间if start_time == ngx.nullor 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)endend--结尾标记::A::local ok, err = cache:close()

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

lua_package_path"/usr/local/nginx/redis.lua;;";

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

access_by_lua_file/usr/local/nginx/conf/access.lua

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

猜你喜欢

转载自hugoren.iteye.com/blog/2214811