nginx+lua+redis實現(xiàn)限流的示例代碼
概述
nginx、lua訪問redis的三種方式
- HttpRedis模塊
指令少,功能單一,適合簡單緩存。只支持get,select命令。 - HttpRedis2Module模塊
功能強大,比較靈活 - lua-resty-redis庫
OpenResty提供的API。適合復(fù)雜業(yè)務(wù),節(jié)省內(nèi)存。
以上3個模塊OpenResty都有集成
OpenResty: 基于nginx開源版本的一個擴展版本。集成了大量的精良的lua庫。所以接下來我們就使用OpenResty來實現(xiàn)相應(yīng)的功能
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腳本已經(jīng)通過接下來就可以開始實現(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;
# 需要準備一個被代理的服務(wù)
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秒內(nèi),不能被訪問
-- 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
-- 次數(shù)加1
inc = red:incr("testLimit");
if inc <= 2 then
-- 設(shè)置過期時間 1秒
red:expire("testLimit",1);
else
-- 超過閾值 limit設(shè)置成1 并設(shè)置過期時間10秒
red:set("limit",1);
red:expire("limit", 10);
end
測試
當快速方法時會報503錯誤。10秒后恢復(fù)正常訪問。
到此這篇關(guān)于nginx+lua+redis實現(xiàn)限流的示例代碼的文章就介紹到這了,更多相關(guān)nginx+lua+redis 限流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過Nginx+Tomcat+Redis實現(xiàn)持久會話
這篇文章主要介紹了通過Nginx+Tomcat+Redis實現(xiàn)持久會話的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
Ubuntu下Nginx配置ThinkPHP的Pathinfo和URl Rewrite模式
這篇文章主要介紹了Ubuntu下Nginx配置ThinkPHP的Pathinfo和URl Rewrite模式,Ubuntu下的配置會有一些不同之處,需要的朋友可以參考下2015-07-07
nginx支持codeigniter的pathinfo模式url重寫配置寫法示例
這篇文章主要介紹了nginx支持codeigniter的pathinfo模式url重寫配置寫法示例,pathinfo模式是一種開發(fā)框架都愛用的路由模式,需要的朋友可以參考下2014-07-07

