location /c {
default_type text/plain;
content_by_lua_block {
local index = ngx.shared.index
local count
if(index:get("count") == nil) then
count = 0
else
count = index:get("count")
end
index:set("count",count + 1)
local redis = require 'resty.redis'
local red = redis.new()
red:set_timeout(1000)
local ok, err = red:connect('127.0.0.1', 6379)
ok,err = red:set(count,math.random(100000))
ngx.say('ok...' .. ok)
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
}
}
openresty 链接redis配置
最新推荐文章于 2025-03-24 22:18:12 发布
这篇博客探讨了如何使用Lua脚本在Nginx中实现一个简单的计数器功能。通过Nginx的`content_by_lua_block`指令,访问共享内存变量`ngx.shared.index`来初始化和更新计数。同时,文章展示了如何连接并操作Redis服务器,设置键值对,以及保持Redis连接的持久化。内容涉及到 Lua 脚本和 Redis 的基本用法,对于理解Web服务器与缓存系统的集成具有一定的参考价值。
2万+

被折叠的 条评论
为什么被折叠?



