安裝Nginx Lua Redis模塊防止CC攻擊教程

Nginx?Lua?Redis防止CC攻擊實(shí)現(xiàn)原理:同一個外網(wǎng)IP、同一個網(wǎng)址(ngx.var.request_uri)、同一個客戶端(http_user_agent)在某一段時間(CCseconds)內(nèi)訪問某個網(wǎng)址(ngx.var.request_uri)超過指定次數(shù)(CCcount),則禁止這個外網(wǎng)IP+同一個客戶端(md5(IP+ngx.var.http_user_agent)訪問這個網(wǎng)址(ngx.var.request_uri)一段時間(blackseconds)。該腳本使用lua編寫(依賴nginx+lua),將信息寫到redis(依賴redis.lua)。

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規(guī)則waf.lua

將下面內(nèi)容保存在/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虛擬主機(jī)加載waf.lua

在虛擬主機(jī)配置文件/usr/local/nginx/conf/vhost/oneinstack.com.conf

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

測試

一分鐘之內(nèi),點(diǎn)擊20此以上同一個頁面,登錄redis,看到black key開通的即被禁止訪問(nginx 503)

搬瓦工推薦方案

溫馨提醒 如果您有選擇困難癥,直接選中間的 CN2 GIA-E方案,季付 $49.99,多達(dá) 12 個機(jī)房任意切換
方案 內(nèi)存 CPU 硬盤 流量/月 帶寬 機(jī)房 價格 購買
CN2
(最便宜)
1GB 1核 20GB 1TB 1Gbps DC3 CN2
DC8 ZNET
$49.99/年 直達(dá)
CN2 2GB 1核 40GB 2TB 1Gbps $52.99/半年
$99.99/年
直達(dá)
CN2 GIA-E
(最推薦)
1GB 2核 20GB 1TB 2.5Gbps DC6 CN2 GIA-E
DC9 CN2 GIA
日本軟銀 JPOS_1
荷蘭 EUNL_9
$49.99/季度
$169.99/年
直達(dá)
CN2 GIA-E 2GB 3核 40GB 2TB 2.5Gbps $89.99/季度
$299.99/年
直達(dá)
HK 2GB 2核 40GB 0.5TB 1Gbps 中國香港 CN2 GIA $89.99/月
$899.99/年
直達(dá)
HK 4GB 4核 80GB 1TB 1Gbps $155.99/月
$1559.99/年
直達(dá)
TOKYO 2GB 2核 40GB 0.5TB 1.2Gbps 日本東京 CN2 GIA $89.99/月
$899.99/年
直達(dá)
TOKYO 4GB 4核 80GB 1TB 1.2Gbps $155.99/月
$1559.99/年
直達(dá)
搬瓦工優(yōu)惠碼:BWH3HYATVBJW 2022年最新搬瓦工VPS購買圖文教程
贊(0)
版權(quán)聲明:本文采用知識共享 署名4.0國際許可協(xié)議 [BY-NC-SA] 進(jìn)行授權(quán), 轉(zhuǎn)載請注明出處。
文章名稱:《安裝Nginx Lua Redis模塊防止CC攻擊教程》
文章鏈接:http://www.lrracing.com/38051.html
【聲明】:國外主機(jī)測評僅分享信息,不參與任何交易,也非中介,所有內(nèi)容僅代表個人觀點(diǎn),均不作直接、間接、法定、約定的保證,讀者購買風(fēng)險自擔(dān)。一旦您訪問國外主機(jī)測評,即表示您已經(jīng)知曉并接受了此聲明通告。
【關(guān)于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠(yuǎn)是最佳選擇,服務(wù)器也是機(jī)器,不勤備份是對自己極不負(fù)責(zé)的表現(xiàn),請保持良好的備份習(xí)慣。