Reputation: 9828
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.
Upvotes: 1
Views: 7940
Reputation: 12775
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
';
}
Upvotes: 3
Reputation: 9828
I used req.get_uri_args() to get all parameters passed in url.
local args = ngx.req.get_uri_args()
Upvotes: 3