1

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 Answers 2

3

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

local args = ngx.req.get_uri_args()
Sign up to request clarification or add additional context in comments.

3 Comments

It didn't work for me.. it gives 500 internal server error. Can you share how you did it in more detail please?
Please tell me with which context you are using this .? you can use this only with set_by_lua, rewrite_by_lua, access_by_lua, content_by_lua, header_filter_by_lua, body_filter_by_lua, log_by_lua
I resolved it by using ngx.var.uri() instead. Thank you anyway :)
3

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
    ';
}

Comments

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.