Modify cookie attributes:
Version one:
header_filter_by_lua '
local cookies = ngx.header.set_cookie
if cookies then
if type(cookies) == "table" then
for k,v in pairs(cookies) do
ngx.log(ngx.INFO, "k:"..k..",value:"..v)
cookies[k] = string.gsub(v, "test", "hello")
ngx.header.set_cookie = cookies
end
else
ngx.log(ngx.INFO, "cookie:"..cookies)
local cookiesStr = string.gsub(cookies, "test", "hello")
ngx.header.set_cookie = cookiesStr
end
end
';
Version two:
header_filter_by_lua '
local cookies = ngx.header["set-cookie"]
if cookies then
if type(cookies) == "table" then
for k,v in pairs(cookies) do
ngx.log(ngx.INFO, "k:"..k..",value:"..v)
cookies[k]=string.gsub(v, "test", "hello")
ngx.header.["set-cookie"] = cookies
end
else
ngx.log(ngx.INFO, "cookies:"..cookies)
local cookiesStr = string.gsub(cookies, "test", "hello")
ngx.header.["set-cookie"] = cookiesStr
end
end
';
Description:
1. The above example searches for the "test" string in the cookie attribute in the response header, and replaces it with the "hello" string when found.
2. There are multiple same attributes in the response header: if type(cookies) == "table" then