首页 文章

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

提问于
浏览
1

我有一个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有一个问题 .
请帮我解决这个问题 .

1 回答

  • 0

    如果您只想提供fcgi中的内容,请使用internal redirection . 如果你还想从Lua填充缓存,你应该看看subrequests .

相关问题