openResty簡(jiǎn)介:
OpenResty??是一個(gè)基于?Nginx?與 Lua 的高性能 Web 平臺(tái),其內(nèi)部集成了大量精良的 Lua 庫(kù)、第三方模塊以及大多數(shù)的依賴(lài)項(xiàng)。用于方便地搭建能夠處理超高并發(fā)、擴(kuò)展性極高的動(dòng)態(tài) Web 應(yīng)用、Web 服務(wù)和動(dòng)態(tài)網(wǎng)關(guān)。
OpenResty??通過(guò)匯聚各種設(shè)計(jì)精良的?Nginx?模塊(主要由 OpenResty 團(tuán)隊(duì)自主開(kāi)發(fā)),從而將?Nginx?有效地變成一個(gè)強(qiáng)大的通用 Web 應(yīng)用平臺(tái)。這樣,Web 開(kāi)發(fā)人員和系統(tǒng)工程師可以使用 Lua 腳本語(yǔ)言調(diào)動(dòng)?Nginx?支持的各種 C 以及 Lua 模塊,快速構(gòu)造出足以勝任 10K 乃至 1000K 以上單機(jī)并發(fā)連接的高性能 Web 應(yīng)用系統(tǒng)。
OpenResty??的目標(biāo)是讓你的Web服務(wù)直接跑在?Nginx?服務(wù)內(nèi)部,充分利用?Nginx?的非阻塞 I/O 模型,不僅僅對(duì) HTTP 客戶(hù)端請(qǐng)求,甚至于對(duì)遠(yuǎn)程后端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進(jìn)行一致的高性能響應(yīng)。
1、下載安裝openresty
下載安裝openresty。我使用的如下版本的
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
解壓文件:tar -zvxf?openresty-1.21.4.1.tar.gz
進(jìn)入文件夾下:cd ./openresty-1.21.4.1
執(zhí)行./configure
然后 make && make install
等待以上完成后。就可以到啟動(dòng)了。
具體啟動(dòng)的nginx服務(wù)目錄在?openresty-1.21.4.1/nginx/sbin下。
啟動(dòng)命令如下:
./openresty-1.21.4.1/nginx/sbin/nginx -c ./openresty-1.21.4.1/nginx/conf/nginx.conf
2、配置lua文件
在nginx.conf中配置限流lua文件。
在你要限流的接口下增加。
location /xxx{
access_by_lua_file /x/x/x/access_by_redis.lua;
proxy_pass http://xxxx/;
}
3、編寫(xiě)lua限流內(nèi)容
access_by_redis.lua文件內(nèi)容如下:
local function close_redis(red)
? ? if not red then
? ? ? ? return
? ? end
? ? --釋放連接(連接池實(shí)現(xiàn))
? ? local pool_max_idle_time = 10000 --毫秒
? ? local pool_size = 100 --連接池大小
? ? local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
? ? if not ok then
? ? ? ? ngx_log(ngx_ERR, "set redis keepalive error : ", err)
? ? end
end
-- 連接redis
local redis = require('resty.redis')
local red = redis.new()
red:set_timeout(1000)
local ip = "127.0.0.1" ?---修改變量
local port = "6379" ---修改變量
local ok, err = red:connect(ip,port)
if not ok then
? ? return close_redis(red)
end
red:auth('密碼')
local clientIP = ngx.req.get_headers()["X-Real-IP"]
if clientIP == nil then
? ?clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
if clientIP == nil then
? ?clientIP = ngx.var.remote_addr
end
--ngx.say(clientIP)
--if clientIP == "101.231.137.70" then
-- ? ?ngx.exit(ngx.HTTP_FORBIDDEN)
-- ? ? ? return close_redis(red)
-- ? ?end
local request_uri = ngx.var.request_uri
-- 限制條件:user:用戶(hù)ip:請(qǐng)求地址。做限制條件
local incrKey = "user:"..clientIP..":"..request_uri..":freq"
local blockKey = "user:"..clientIP..":block"
local is_block,err = red:get(blockKey) -- check if ip is blocked
--ngx.say(tonumber(is_block))
if tonumber(is_block) == 1 then
? ? --ngx.say(3)
? ? ngx.exit(403)
? ? --ngx.exit(ngx.HTTP_FORBIDDEN)
? ? close_redis(red)
end
inc ?= red:incr(incrKey)
--5秒內(nèi)有15次以上訪問(wèn)即視為非法,會(huì)阻止1分鐘的訪問(wèn)
if inc < 15 then
? ?inc = red:expire(incrKey,5)
end
if inc > 15 then
? ? red:set(blockKey,1) --設(shè)置block 為 True 為1
? ? red:expire(blockKey,60)
end文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-678158.html
close_redis(red)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-678158.html
到了這里,關(guān)于openResty+lua+redis實(shí)現(xiàn)接口訪問(wèn)頻率限制的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!