nginx lua redis java_在openresty中使用nginx的Lua:如果在redis缓存中找不到数据,则将请求传递给FastCGI...

我有一个Django网站,它使用fcgi在Nginx上运行 .

对于url /gifts/ 我想使用openresty在nginx.conf文件中的lua中实现一些逻辑 .

location /gifts {

try_files $uri @redis_cache;

}

location @redis_cache {

default_type text/html;

content_by_lua '

-- fetching key and values from url

local args = ngx.req.get_uri_args()

--creating redis connection

local redis = require "resty.redis";

local red = redis:new()

red:set_timeout(1000) -- 1 sec

local ok, err = red:connect("127.0.0.1", 6379)

if not ok then

ngx.log(ngx.ERR, err, "Redis failed to connect")

return ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)

end

if not args["key"] then

return ngx.exit(ngx.HTTP_NOT_FOUND)

end

if args["value"] then

local ok, err = red:set(args["key"], args["value"])

end

if not ok then

ngx.say("Please pass key value pair to store in cache", err)

end

-- getting data from redis cache

local res, err = red:get(args["key"])

if not res then

return ngx.say("value is not in redis cache", err, "|")

end

ngx.say("Value found in Redis is: ", res)

';

}

根据要求,Everythig工作正常,但如果缓存不可用于Redis,我想将请求重定向到fcgi有一个问题 .

请帮我解决这个问题 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值