Nginx+Lua+Redis实例
2014-03-04 12:31
459 查看
lua加入到nginx中,相当于让nginx拥有了处理业务逻辑的能力。
例如:对IP进行封禁
在站点遇到攻击且无明显攻击特征,造成站点访问慢,nginx不断返回502等错误时,可利用nginx+lua+redis实现在指定的时间段内,若单IP的请求量达到指定的数量后对该IP进行封禁,nginx返回403禁止访问。利用redis的expire命令设置封禁IP的过期时间达到在指定的封禁时间后实行自动解封的目的。
例如:灰度发布
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB
test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面 来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。
下面是一个nginx+lua+redis实例,在此基础上可以扩展nginx业务能力
test.conf
test.lua
例如:对IP进行封禁
在站点遇到攻击且无明显攻击特征,造成站点访问慢,nginx不断返回502等错误时,可利用nginx+lua+redis实现在指定的时间段内,若单IP的请求量达到指定的数量后对该IP进行封禁,nginx返回403禁止访问。利用redis的expire命令设置封禁IP的过期时间达到在指定的封禁时间后实行自动解封的目的。
例如:灰度发布
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB
test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面 来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。
下面是一个nginx+lua+redis实例,在此基础上可以扩展nginx业务能力
test.conf
server { listen 80; server_name test.geetest.com; location @client{ proxy_pass http://www.baidu.com; } location / { set $variable 23; lua_code_cache off; default_type text/html; content_by_lua_file /usr/local/openresty/nginx/conf/sites-avaliable/test_content.lua; access_by_lua_file /usr/local/openresty/nginx/conf/sites-avaliable/test_access.lua; rewrite_by_lua_file /usr/local/openresty/nginx/conf/sites-avaliable/test_rewrite.lua; } }
test.lua
local redis = require "resty.redis" local red = redis:new() red:set_timeout(1000) -- 1 sec -- or connect to a unix domain socket file listened -- by a redis server: -- local ok, err = red:connect("unix:/path/to/redis.sock") local ok, err = red:connect("127.0.0.1", 6379) if not ok then ngx.say("failed to connect: ", err) return end ok, err = red:set("dog", "an animal") if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", ok) local res, err = red:get("dog") if not res then ngx.say("failed to get dog: ", err) return end if res == ngx.null then ngx.say("dog not found.") return end ngx.say("dog: ", res) red:init_pipeline() red:set("cat", "Marry") red:set("horse", "Bob") red:get("cat") red:get("horse") local results, err = red:commit_pipeline() if not results then ngx.say("failed to commit the pipelined requests: ", err) return end for i, res in ipairs(results) do if type(res) == "table" then if not res[1] then ngx.say("failed to run command ", i, ": ", res[2]) else -- process the table value end else -- process the scalar value end end -- put it into the connection pool of size 100, -- with 10 seconds max idle time local ok, err = red:set_keepalive(10000, 100) if not ok then ngx.say("failed to set keepalive: ", err) return end
相关文章推荐
- nginx通过lua和redis防止CC×××
- nginx+lua+redis构建高并发应用
- Linux 守护进程原理及实例(Redis、Nginx)
- CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据
- nginx+lua+redis自动识别封解禁IP频繁访问
- Redis执行Lua脚本详细实例
- nginx+lua+redis构建高并发应用(centos/rehat)
- Redis如何使用lua脚本实例教程
- nginx lua调用redis和mongo
- nginx+lua+redis实现logserver
- 新博客关于redis、git、nginx、mongodb、lua、python等热文
- 【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)
- nginx+lua+redis 使用方法
- nginx嵌入lua脚本操作redis
- nginx + lua +redis环境搭建
- nginx+lua+redis(openresty)配置
- nginx+lua+redis实现logserver
- nginx+lua+redis实现验证码防采集
- nginx+lua+redis构建高并发应用
- 使用Nginx+Lua+Redis进行Web开发