1
votes

I am having a url '/gifts/' below is code of nginx.conf file which is managing logic.

location /gifts {
    default_type text/html;
    set $target '';
    content_by_lua '
        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
        local ok1, err = red:set("Animal", "DOG")
        if not ok then
            ngx.say("Failed to set cache in redis", err)
        end 
        local res, err = red:get("Animal")
        if not res then
            ngx.say("Failure", err)
        end
        ngx.say("Animal", res)
';
}

It is working fine for me with /gifts/ But i have one requirement like i want to fetch parameters into this login like /gifts?key=name&value=Prashant I want to fetch value of key and value.

2

2 Answers

3
votes

I used req.get_uri_args() to get all parameters passed in url.

local args = ngx.req.get_uri_args()
3
votes

Take a look at ngx.req.get_uri_args(), this will return a Lua table holding all the current request URL query arguments.

Example:

 location = /test {
    content_by_lua '
        local args = ngx.req.get_uri_args()
        for key, val in pairs(args) do
            if type(val) == "table" then
                ngx.say(key, ": ", table.concat(val, ", "))
            else
                ngx.say(key, ": ", val)
            end
        end
    ';
}