为什么我不能设置多个cookies

Dat*_*Boi 5 openresty

我正在尝试设置多个 cookie,但它不起作用:

if type(ngx.header["Set-Cookie"]) ~= "table" then
    ngx.header["Set-Cookie"] = {}
end
table.insert(ngx.header["Set-Cookie"], "Cookie1=abc; Path=/")
table.insert(ngx.header["Set-Cookie"], "Cookie2=def; Path=/")
table.insert(ngx.header["Set-Cookie"], "Cookie3=ghi; Path=/")
Run Code Online (Sandbox Code Playgroud)

在客户端,我没有收到任何 cookie。

wil*_*lin 6

ngx.header["Set-Cookie"] 是一个特殊的表,每次修改它时都必须用新表重新分配(插入或从中删除的元素对将发送到客户端的 cookie 没有影响):

if type(ngx.header["Set-Cookie"]) == "table" then
    ngx.header["Set-Cookie"] = { "AnotherCookieValue=abc; Path=/", unpack(ngx.header["Set-Cookie"]) }
else
    ngx.header["Set-Cookie"] = { "AnotherCookieValue=abc; Path=/", ngx.header["Set-Cookie"] }
end
Run Code Online (Sandbox Code Playgroud)