openResty+redis的方式實現(xiàn)ip黑名單

安裝依賴庫,注意perl版本需要5.6.1+

[root@centos7-template openresty-1.19.3.1]# yum install perl libpcre libssl gcc pcre-devel openssl openssl-devel -y

下載openResty,官網(wǎng):http://openresty.org/

[root@centos7-template data]# wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
[root@centos7-template data]# tar -xf openresty-1.19.3.1.tar.gz

修改nginx版本信息(此步可以忽略)

[root@centos7-template data]# vim ./openresty-1.19.3.1/bundle/nginx-1.19.3/src/core/nginx.h

##以下為修改內(nèi)容##
#將#define NGINX_VER          "openresty/" NGINX_VERSION ".1"
#修改為#define NGINX_VER          "ylw/" NGINX_VERSION ".1"

編譯openResty,編譯成功后,默認(rèn)安裝路徑在/usr/local/openresty

[root@centos7-template openresty-1.19.3.1]# ./configure && make && make install

進(jìn)入配置openResty的nginx

[root@ylw conf]# vim /usr/local/openresty/nginx/conf/nginx.conf

##下面為配置內(nèi)容
#1、http段加入下面配置
   lua_package_path "/usr/local/openresty/lualib/?.lua;;" ;#加載lua庫
   lua_package_cpath "/usr/local/openresty/lualib/?.so;;" ;#加載c庫
   server_tokens off;#隱藏版本號
   autoindex off;#關(guān)閉目錄瀏覽功能
#2、server段加入下面配置
    error_page 403 /403.html;#當(dāng)狀態(tài)碼為403時,跳轉(zhuǎn)到指定location
    location /403.html { #自定義403錯誤頁面,頁面需要自己創(chuàng)建
        root html;
    }
#3、location /段加入如下配置
    access_by_lua_file /usr/local/openresty/luajit/sh/test.lua;#在處理請求階段,執(zhí)行l(wèi)ua腳本

編寫lua腳本,記得腳本要給x權(quán)限

[root@ylw openresty]# vim /usr/local/openresty/luajit/sh/test.lua

#下面為腳本內(nèi)容
-- 連接redis檢查ip是否在redis中
local function check_ip(ip)
        local redis = require "resty.redis"
        local red = redis:new()

        red:set_timeout(redis_connection_timeout)-- Time out

        local ok, err = red:connect("127.0.0.1",6379)-- 地址host,端口號port
        ok, err = red:auth("123123")-- password    -- redis設(shè)置的密碼
        if not ok then    -- 鏈接失敗的時候
                ngx.log(ngx.DEBUG, "error:" .. err)
        else
                local test = red:exists(ip)-- 判斷redis中key值是否存在,存在則返回1,不存在則返回0
                if test == 1 then
            ngx.exit(403)
            lua_exit()
        end
    end
end

-- 字符串切割函數(shù)
function split(szFullString, szSeparator)  
 local nFindStartIndex = 1  
 local nSplitIndex = 1  
 local nSplitArray = {}  
 while true do  
    local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)  
    if not nFindLastIndex then  
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))  
        break  
    end  
    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)  
    nFindStartIndex = nFindLastIndex + string.len(szSeparator)  
    nSplitIndex = nSplitIndex + 1  
    end  
    return nSplitArray  
end  
-- 切割http_x_forwarded_for函數(shù)
local function cutString(str1)
    local ip_list = split(str1,",")
        return ip_list
end

if ngx.var.http_x_forwarded_for == nil then -- 當(dāng)不經(jīng)過代理直接訪問時,http_x_forwarded_for值為空,此時判斷remote_addr是否在黑名單中。(不判空的話,為空時程序會報錯)
    check_ip(ngx.var.remote_addr)
else
    for i,v in pairs(cutString(string.gsub(ngx.var.http_x_forwarded_for," ",""))) do -- 字符串切割會返回一個table,采用for循環(huán)遍歷table。同時,記得先處理ngx.var.http_x_forwarded_for值帶有空格的問題
        check_ip(v)
    end
end

搭建redis

[root@centos7-template data]# tar -xf redis-4.0.14.tar.gz
[root@centos7-template data]# cd redis-4.0.14
[root@centos7-template redis-4.0.14]# make && make install

修改redis配置文件

requirepass 123123 #設(shè)置密碼
daemonize yes #后臺啟動

啟動redis

[root@ylw redis-4.0.14]# redis-server ./redis.conf

登錄redis,并且設(shè)置key值(模擬ip黑名單)

[root@ylw redis-4.0.14]# redis-cli -h 127.0.0.1 -p 6379 -a 123123
127.0.0.1:6379> set 192.168.150.1 1

啟動openresty的nginx

[root@ylw sbin]# /usr/local/openresty/nginx/sbin/nginx

測試步驟

1、redis的key值中存在你的ip地址,訪問系統(tǒng),出現(xiàn)403則表示攔截成功

2、刪除redis中你的ip地址,訪問系統(tǒng),看看是否能否正常訪問

ps:

1、lua腳本做修改時,必須重載nginx配置,否則新lua腳本不生效

2、命令行登錄redis后,通過keys *的方式查看所有keys,keys xxx 可以查看具體的key。

3、cat ip_test.txt | redis-cli -h 127.0.0.1 -p 6379 -a 123123 --pipe,此命令可以批量執(zhí)行redis命令

4、del key的名字,此命令可以刪除redis中的key
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容