2

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 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. can you provide any example code for subrequests .thank you
Something like: local value = ngx.location.capture("/my_fcgi_url").body; red:set(args["key"], value); return ngx.say("Value from FCGI: ", value) Of course add error checking etc. Read the ngx.location.capture documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.