【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的安装

shell 复制代码
# 安装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配置

shell 复制代码
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脚本代码块
shell 复制代码
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脚本的日志文件
shell 复制代码
# 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;
		}
	}
}
lua 复制代码
-- ip_limit_access.lua
ngx.say("ip limit lua");

先编写一个简单的脚本测试 看看是否配置成功。

重启nginx并加载指定配置

shell 复制代码
# 查看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脚本已经通过接下来就可以开始实现限流功能了。

整体思路

编写nginx配置

编写配置ngin-ip-limit.conf

shell 复制代码
# 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日志脚本

lua 复制代码
-- ip_limit_log.lua
local ip = ngx.var.remote_addr;
ngx.log(ngx.INFO, "request ip is:"..ip);

编写lua限流脚本

需求:系统每秒限流2个请求,如果超过阈值(每秒2个请求),则系统限制10秒内,不能被访问

lua 复制代码
-- 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秒后恢复正常访问。

相关推荐
还是大剑师兰特1 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
IT大白鼠1 天前
Redis 系列 · 第 01 篇——认知入门:Redis 是什么
redis·nosql
彧azz1 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
xing-xing1 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
lbb 小魔仙1 天前
OpenClaw + cpolar 实战:远程 NAS、分享小游戏、RDP,再配置公网 AI 入口
数据库·人工智能·redis·oracle·prometheus
夕除1 天前
redis--018
redis
PC2005-cloud1 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
两锭子1 天前
Redis基础
redis
烟沙九洲1 天前
Redis 缓存穿透、缓存击穿、缓存雪崩
数据库·redis
字节探索1 天前
Redis 只会当缓存用?这 10 大实战场景,让你的系统快到飞起
redis·后端