当前位置: 首页 > news >正文

【Nginx】nginx+lua+redis实现限流

概述

nginx、lua访问redis的三种方式

  1. HttpRedis模块
    指令少,功能单一,适合简单缓存。只支持get,select命令。
  2. HttpRedis2Module模块
    功能强大,比较灵活
  3. lua-resty-redis库
    OpenResty提供的API。适合复杂业务,节省内存。
    以上3个模块OpenResty都有集成

OpenResty: 基于nginx开源版本的一个扩展版本。集成了大量的精良的lua库。所以接下来我们就使用OpenResty来实现相应的功能

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脚本已经通过接下来就可以开始实现限流功能了。

整体思路

Alt

编写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限流脚本

需求:系统每秒限流2个请求,如果超过阈值(每秒2个请求),则系统限制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' thenreturn 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秒后恢复正常访问。

http://www.dtcms.com/a/285495.html

相关文章:

  • 【Lua】闭包可能会导致的变量问题
  • 去中心化交易所(DEX)深度解析:解码行业头部项目
  • 双向广搜算法详解
  • 【现有资料整理】灵枢 - 用于医学领域的 SOTA 多模态大语言模型
  • 对Yii2中开启`authenticator`后出现的跨域问题-修复
  • .QOI: Lossless Image Compression in O(n) Time
  • 变量命名规则
  • git--gitlab
  • 性能远超Spring Cloud Gateway!Apache ShenYu如何重新定义API网关!
  • 无标记点动捕:如何突破传统娱乐边界,打造沉浸式交互体验
  • 高速公路自动化安全监测主要内容
  • Elasticsearch+Logstash+Filebeat+Kibana部署(单机部署)
  • 在 Jenkins 中使用 SSH 部署密钥
  • JAVA高级第五章,简易超市会员管理系统
  • sqli-labs靶场通关笔记:第29-31关 HTTP参数污染
  • Android 应用保活思路
  • 小红书采集工具:无水印图片一键获取,同步采集笔记与评论
  • 银河麒麟高级服务器V10(ARM)安装人大金仓KingbaseES完整教程
  • 【unitrix】 6.7 基本结构体(types.rs)
  • IDEA插件离线安装
  • Vue3 Anime.js超级炫酷的网页动画库详解
  • 完整的 Meteor NPM 集成
  • 游戏常用运行库合集下载 - 提升游戏与软件体验
  • Nestjs框架: 基于TypeORM的多租户功能集成
  • Linux C 进程间通信基本操作
  • QT Windows 资源管理器的排序规则
  • 通俗易懂:什么是决策树?
  • 禁止拖动视频进度条来保障视频安全?
  • MBIST - Memory BIST会对memory进行清零吗?
  • QGIS二次开发环境搭建(qgis-3.28.6+qt5.15)