nginx+lua+redis實現(xiàn)限流的示例代碼
概述
nginx、lua訪問redis的三種方式
- HttpRedis模塊
指令少,功能單一,適合簡單緩存。只支持get,select命令。 - HttpRedis2Module模塊
功能強大,比較靈活 - lua-resty-redis庫
OpenResty提供的API。適合復雜業(yè)務,節(jié)省內存。
以上3個模塊OpenResty都有集成
OpenResty: 基于nginx開源版本的一個擴展版本。集成了大量的精良的lua庫。所以接下來我們就使用OpenResty來實現(xiàn)相應的功能
OpenResty的安裝
# 安裝wget 如果沒有的話 yum install wget # 下載資源庫 這樣yum就可以直接安裝了 # 得到 openresty.repo cd /etc/yum.repos.d/ wget https://openresty.org/package/centos/openresty.repo # 安裝openresty 安裝目錄:/usr/local/openresty yum install openresty
編寫nginx配置
cd /usr/local/openresty/nginx/conf vim nginx-lua.conf
openresty 提供了幾種方式來配置lua腳本需要先了解一下
content_by_lua 'ngx.say("hello my openrestry")': 可以直接在配置文件中寫入單行的lua腳本的字符串。content_by_lua_block:可以在nginx配置文件中配置 多行的 lua腳本代碼塊
content_by_lua_block {
ngx.say("hello");
ngx.say("block");
}
content_by_lua_file /usr/local/openresty/nginx/lua/lua-test.lua: 可以配置lua腳本的文件路徑log_by_lua_file /usr/local/openresty/nginx/lua/lua-log-test.lua: 可以配置lua腳本的日志文件
# nginx-lua.conf
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
# 連接的超時時間
keepalive_timeout 65;
server {
listen 8080;
location / {
default_type text/html;
# 此處可以寫lua腳本的文件路徑
content_by_lua_file /usr/local/openresty/nginx/lua/ip_limit_log.lua;
}
}
}
-- ip_limit_access.lua
ngx.say("ip limit lua");
先編寫一個簡單的腳本測試 看看是否配置成功。
重啟nginx并加載指定配置
# 查看nginx是否啟動 ps -ef | grep nginx # 停止nginx /usr/local/openresty/nginx/sbin/nginx -s stop # 啟動nginx -p指定工作目錄 -c 指定配置文件 /usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf # 使用curl訪問地址 測試成功 [root@localhost nginx]# curl http://localhost ip limit lua
nginx_lua_redis限流
通過以上測試,nginx配置lua腳本已經通過接下來就可以開始實現(xiàn)限流功能了。
整體思路

編寫nginx配置
編寫配置ngin-ip-limit.conf
# ngin-ip-limit.conf
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
localtion / {
default_type text/html;
# 配置lua腳本文件路徑
access_by_lua_file /usr/local/openresty/nginx/lua/ip_limit_access.lua;
# 配置lua日志腳本路徑
log_by_lua_file /usr/local/openresty/nginx/lua/ip_limit_log.lua;
# 需要準備一個被代理的服務
proxy_pass http://localhost:8080/;
}
}
}
編寫lua日志腳本
-- ip_limit_log.lua local ip = ngx.var.remote_addr; ngx.log(ngx.INFO, "request ip is:"..ip);
編寫lua限流腳本
需求:系統(tǒng)每秒限流2個請求,如果超過閾值(每秒2個請求),則系統(tǒng)限制10秒內,不能被訪問
-- ip_limit_access.lua
ngx.log(ngx.INFO, "ip limit log");
local redis = require "resty.redis";
local red = redis:new();
-- 連接redis
red:connect("127.0.0.1",6379);
-- 判斷是否限流
limit = red:get("limit");
if limit == '1' then
return ngx.exit(503);
end
-- 次數加1
inc = red:incr("testLimit");
if inc <= 2 then
-- 設置過期時間 1秒
red:expire("testLimit",1);
else
-- 超過閾值 limit設置成1 并設置過期時間10秒
red:set("limit",1);
red:expire("limit", 10);
end
測試
當快速方法時會報503錯誤。10秒后恢復正常訪問。
到此這篇關于nginx+lua+redis實現(xiàn)限流的示例代碼的文章就介紹到這了,更多相關nginx+lua+redis 限流內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx內網環(huán)境開啟https雙協(xié)議的實現(xiàn)
本文主要介紹了Nginx內網環(huán)境開啟https雙協(xié)議,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02

