在 Openresty 中删除多个动态命名的 cookie Lua

Delete multiple dynamically named cookies in Openresty Lua

下面的代码在 Openresty 中工作正常 lua

ngx.header["Set-Cookie"] = {
   'test1=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;',
   'test2=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;'
}

尽管尝试使 cookie 名称动态化时,它不起作用:

local cookies = {}
local args = {'test1', 'test2'}

for i=1, #args do
  cookies[i] = args[i] .. '=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;'
end

ngx.header["Set-Cookie"] = cookies

甚至尝试过使用 table.insert:

local cookies = {}
local args = {'test1', 'test2'}

for i=1, #args do
  table.insert(cookies, args[i] .. '=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;')
end

ngx.header["Set-Cookie"] = cookies

问题似乎是将变量赋值给 ngx.header["Set-Cookie"]

使用 ngx.header.set_cookie 代替 ngx.header["Set-Cookie"] 是可行的。以下代码现在有效:

local cookies = {}
local args = {'test1', 'test2'}

for i=1, #args do
  cookies[i] = args[i] .. '=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;'
end

ngx.header.set_cookie = cookies