安裝依賴(lài)庫(kù),注意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庫(kù)
lua_package_cpath "/usr/local/openresty/lualib/?.so;;" ;#加載c庫(kù)
server_tokens off;#隱藏版本號(hào)
autoindex off;#關(guān)閉目錄瀏覽功能
#2痘煤、server段加入下面配置
error_page 403 /403.html;#當(dāng)狀態(tài)碼為403時(shí),跳轉(zhuǎn)到指定location
location /403.html { #自定義403錯(cuò)誤頁(yè)面拌消,頁(yè)面需要自己創(chuàng)建
root html;
}
#3、location /段加入如下配置
access_by_lua_file /usr/local/openresty/luajit/sh/test.lua;#在處理請(qǐng)求階段,執(zhí)行l(wèi)ua腳本
編寫(xiě)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,端口號(hào)port
ok, err = red:auth("123123")-- password -- redis設(shè)置的密碼
if not ok then -- 鏈接失敗的時(shí)候
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)過(guò)代理直接訪(fǎng)問(wèn)時(shí),http_x_forwarded_for值為空延届,此時(shí)判斷remote_addr是否在黑名單中剪勿。(不判空的話(huà),為空時(shí)程序會(huì)報(bào)錯(cuò))
check_ip(ngx.var.remote_addr)
else
for i,v in pairs(cutString(string.gsub(ngx.var.http_x_forwarded_for," ",""))) do -- 字符串切割會(huì)返回一個(gè)table方庭,采用for循環(huán)遍歷table厕吉。同時(shí),記得先處理ngx.var.http_x_forwarded_for值帶有空格的問(wèn)題
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 #后臺(tái)啟動(dòng)
啟動(dòng)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
啟動(dòng)openresty的nginx
[root@ylw sbin]# /usr/local/openresty/nginx/sbin/nginx
測(cè)試步驟
1械念、redis的key值中存在你的ip地址头朱,訪(fǎng)問(wèn)系統(tǒng),出現(xiàn)403則表示攔截成功
2龄减、刪除redis中你的ip地址项钮,訪(fǎng)問(wèn)系統(tǒng),看看是否能否正常訪(fǎng)問(wèn)
ps:
1、lua腳本做修改時(shí)烁巫,必須重載nginx配置鳖敷,否則新lua腳本不生效
2、命令行登錄redis后程拭,通過(guò)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