OpenResty + Lua + Redis 实现 IP 限流 - 简书


本站和网页 https://www.jianshu.com/p/5f9928c7d730 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

OpenResty + Lua + Redis 实现 IP 限流 - 简书登录注册写文章首页下载APP会员IT技术OpenResty + Lua + Redis 实现 IP 限流wmfyt325关注赞赏支持OpenResty + Lua + Redis 实现 IP 限流参考文章
OpenResty® 是一款基于 NGINX 和 LuaJIT 的 Web 平台。OpenResty 官网。
1. 安装
1.1 Mac OS:
Mac OS 通过 Homebrew 安装,执行以下命令:
brew tap openresty/brew
brew install openresty
如果之前通过 Homebrew 安装过 nginx,需要先执行:
brew untap homebrew/nginx
1.2 Linux:
Ubuntu
# 导入 GPG 密钥
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
# 安装 add-apt-respository 命令
sudo apt-get -y install software-properties-common
# 添加官方 official APT 仓库
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
# 更新 APT 索引
sudo apt-get update
# 安装
sudo apt-get install openresty
# 如果不想自动关联安装 openresty-opm 和 openresty-restydoc 包,执行下面的命令
# sudo apt-get install --no-install-recommends openresty
CentOS
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
sudo yum install openresty
Debian
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
sudo apt-get -y install software-properties-common
sudo add-apt-repository -y "deb http://openresty.org/package/debian $(lsb_release -sc) openresty"
sudo apt-get update
sudo apt-get install openresty
# 如果不想自动关联安装 openresty-opm 和 openresty-restydoc 包,执行下面的命令
# sudo apt-get install --no-install-recommends openresty
2.配置 lua 脚本
如果之前安装过 nginx,可以先将 nginx 的配置文件拷贝到 /etc/openresty/ 路径下。
在 /etc/openresty/ 路径下新建 access_by_redis.lua 文件。
# Lua
local function close_redis(red)
if not red then
return
end
-- 释放连接(连接池实现),毫秒
local pool_max_idle_time = 10000
-- 连接池大小
local pool_size = 100
local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
local log = ngx_log
if not ok then
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('123456')
red:select('0')
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
local incrKey = "user:"..clientIP..":freq"
local blockKey = "user:"..clientIP..":block"
local is_block,err = red:get(blockKey) -- check if ip is blocked
if tonumber(is_block) == 1 then
ngx.exit(403)
close_redis(red)
end
inc = red:incr(incrKey)
if inc < 10 then
inc = red:expire(incrKey,1)
end
-- 每秒10次以上访问即视为非法,会阻止1分钟的访问
if inc > 10 then
--设置block 为 True 为1
red:set(blockKey,1)
red:expire(blockKey,60)
end
close_redis(red)
在 nginx 配置文件的 http 段添加 lua 脚本
http {
...
lua_package_path "/usr/local/openresty/lualib/resty/redis.lua;";
...
server 段的 location 中添加
server {
...
location / {
...
access_by_lua_file "/etc/openresty/access_by_redis.lua";
...
...
保存后重新加载 nginx 即可。
追加
lua 脚本出现 nginx no resolver defined to resolve 之类的错误,只需要在 nginx 的 http 块中加入 resolver 8.8.8.8; 即可。
推荐阅读更多精彩内容[code.openresty] Openresty框架-Nginx的lua模块openresty OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量...吃瓜的东阅读 5,074评论 0赞 7Lua系列文章 第一篇 安装OpenResty(Nginx+Lua)开发环境Lua安装 首先我们选择使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了...meng_philip123阅读 2,150评论 0赞 6Lua 5.1 参考手册Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...苏黎九歌阅读 12,944评论 0赞 38「京东开涛」使用Nginx+Lua(OpenResty)开发高性能Web应用几乎所有互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡、反向代理、代理缓存、限流等场景;而把Ng...meng_philip123阅读 9,092评论 3赞 66使用Nginx+Lua(OpenResty)开发高性能Web应用在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡、反向代理、代理缓存、限流等场景;而把Nginx...pure_adoration阅读 3,769评论 4赞 77眼泪很久都不会流泪。 即使妈妈走的那天。 姐姐哭的哀伤,而我几乎是没有波澜的。 但是我还是要哭的。 妈妈走了,不哭的儿...琴语阅读 219评论 3赞 3茫茫人海有个你有人性喜清静,有人却爱热闹;有人经常将简单的事情弄复杂,有人却善于将复杂的事情简单化。这就是人与人之间个性的差异。...唯一还是我阅读 86评论 0赞 0看完《那些年,我们一起追的女孩》,作小诗一首叹 轻吟浅唱萌萌意,雨重声微密密扬。 若许一心长久在,方知世事已非常。 遥知此刻无他乐,强笑开怀是作裳。 异路相逢...道梦鱼阅读 184评论 0赞 0深爱?现实?电台里放着一首不知名的欢快歌曲,我的手机在单曲循环《一生所爱》。听的是《蒙面歌王》里谭维维版本的。原版印象不深,仿...橙ff阅读 123评论 0赞 0评论2赞1616赞17赞1赞赏更多好文