2
votes

I have a Django website which is running on Nginx with fcgi .
For url /gifts/ i want to implement some logic into lua inside nginx.conf file by using openresty .


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 is working fine as per requirement but there is one problem i want to redirect request to fcgi if cache is not available into Redis.
Please help me how to proceed with this.

1

1 Answers

0
votes

If you just want to serve the content from fcgi, use internal redirection. If you also want to fill the cache from Lua you should look at subrequests instead.