站长视角
用户至上

安装Nginx Lua Redis模块防止CC攻击教程

Nginx Lua Redis防止CC攻击实现原理:同一个外网IP、同一个网址(ngx.var.request_uri)、同一个客户端(http_user_agent)在某一段时间(CCseconds)内访问某个网址(ngx.var.request_uri)超过指定次数(CCcount),则禁止这个外网IP+同一个客户端(md5(IP+ngx.var.http_user_agent)访问这个网址(ngx.var.request_uri)一段时间(blackseconds)。该脚本使用lua编写(依赖nginx+lua),将信息写到redis(依赖redis.lua)。

安装Nginx Lua Redis模块防止CC攻击教程

Nginx lua模块安装

重新编译nginx,安装lua模块,或者直接使用OneinStack安装OpenResty自带改模块

pushd /root/oneinstack/src
wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
wget -c http://mirrors.linuxeye.com/oneinstack/src/openssl-1.0.2k.tar.gz
wget -c http://mirrors.linuxeye.com/oneinstack/src/pcre-8.39.tar.gz
wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
git clone https://github.com/simpl/ngx_devel_kit.git
git clone https://github.com/openresty/lua-nginx-module.git
tar xzf nginx-1.10.3.tar.gz
tar xzf openssl-1.0.2k.tar.gz
tar xzf pcre-8.39.tar.gz
tar xzf LuaJIT-2.0.4.tar.gz
pushd LuaJIT-2.0.4
make && make install
popd
pushd nginx-1.10.3
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2k —with-pcre=../pcre-8.39 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3 --add-module=../lua-nginx-module --add-module=../ngx_devel_kit
make
mv /usr/local/nginx/sbin/nginx{,_bk}
cp objs/nginx /usr/local/nginx/sbin
nginx -t #检查语法

加载redis.lua

mkdir /usr/local/nginx/conf/lua
cd /usr/local/nginx/conf/lua
wget https://github.com/openresty/lua-resty-redis/raw/master/lib/resty/redis.lua

在/usr/local/nginx/conf/nginx.conf http { }中添加:

#the Nginx bundle:
lua_package_path "/usr/local/nginx/conf/lua/redis.lua;;";

防止CC规则waf.lua

将下面内容保存在/usr/local/nginx/conf/lua/waf.lua

local get_headers = ngx.req.get_headers
local ua = ngx.var.http_user_agent
local uri = ngx.var.request_uri
local url = ngx.var.host .. uri
local redis = require 'redis' 
local red = redis.new()
local CCcount = 20 
local CCseconds = 60 
local RedisIP = '127.0.0.1' 
local RedisPORT = 6379 
local blackseconds = 7200 

if ua == nil then
   ua = "unknown"
end

if (uri == "/wp-admin.php") then
   CCcount=20
   CCseconds=60
end

red:set_timeout(100) 
local ok, err = red.connect(red, RedisIP, RedisPORT)

if ok then
   red.connect(red, RedisIP, RedisPORT)
 
   function getClientIp()
       IP = ngx.req.get_headers()["X-Real-IP"]
   if IP == nil then
       IP = ngx.req.get_headers()["x_forwarded_for"]
   end
   if IP == nil then
       IP = ngx.var.remote_addr 
   end
   if IP == nil then
       IP = "unknown"
   end
   return IP
   end

   local token = getClientIp() .. "." .. ngx.md5(url .. ua)
   local req = red:exists(token)
   if req == 0 then
       red:incr(token)
       red:expire(token,CCseconds)
   else
       local times = tonumber(red:get(token))
       if times >= CCcount then
           local blackReq = red:exists("black." .. token)
               if (blackReq == 0) then
                   red:set("black." .. token,1)
                   red:expire("black." .. token,blackseconds)
                   red:expire(token,blackseconds)
                   ngx.exit(503)
               else
                   ngx.exit(503)
               end
               return
           else
               red:incr(token)
           end
       end
   return
end

Nginx虚拟主机加载waf.lua

在虚拟主机配置文件/usr/local/nginx/conf/vhost/oneinstack.com.conf

access_by_lua_file "/usr/local/nginx/conf/lua/waf.lua";

测试

一分钟之内,点击20此以上同一个页面,登录redis,看到black key开通的即被禁止访问(nginx 503)

搬瓦工推荐方案

温馨提醒 如果您有选择困难症,直接选中间的 CN2 GIA-E方案,季付 $49.99,多达 12 个机房任意切换
方案 内存 CPU 硬盘 流量/月 带宽 机房 价格 购买
CN2
(最便宜)
1GB 1核 20GB 1TB 1Gbps DC3 CN2
DC8 ZNET
$49.99/年 直达
CN2 2GB 1核 40GB 2TB 1Gbps $52.99/半年
$99.99/年
直达
CN2 GIA-E
(最推荐)
1GB 2核 20GB 1TB 2.5Gbps DC6 CN2 GIA-E
DC9 CN2 GIA
日本软银 JPOS_1
荷兰 EUNL_9
$49.99/季度
$169.99/年
直达
CN2 GIA-E 2GB 3核 40GB 2TB 2.5Gbps $89.99/季度
$299.99/年
直达
HK 2GB 2核 40GB 0.5TB 1Gbps 中国香港 CN2 GIA $89.99/月
$899.99/年
直达
HK 4GB 4核 80GB 1TB 1Gbps $155.99/月
$1559.99/年
直达
TOKYO 2GB 2核 40GB 0.5TB 1.2Gbps 日本东京 CN2 GIA $89.99/月
$899.99/年
直达
TOKYO 4GB 4核 80GB 1TB 1.2Gbps $155.99/月
$1559.99/年
直达
搬瓦工优惠码:BWH3HYATVBJW 2022年最新搬瓦工VPS购买图文教程
赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权, 转载请注明出处。
文章名称:《安装Nginx Lua Redis模块防止CC攻击教程》
文章链接:https://www.veidc.com/38051.html
【声明】:国外主机测评仅分享信息,不参与任何交易,也非中介,所有内容仅代表个人观点,均不作直接、间接、法定、约定的保证,读者购买风险自担。一旦您访问国外主机测评,即表示您已经知晓并接受了此声明通告。
【关于安全】:任何 IDC商家都有倒闭和跑路的可能,备份永远是最佳选择,服务器也是机器,不勤备份是对自己极不负责的表现,请保持良好的备份习惯。

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册