Nginx has a known issue that if multiple Set-Cookie header is set, then Nginx can log only one of them(still not sure whether it's first or random one). So for example if backend sets

"CSESSIONID=XXXXXXXXXXXXXXX"
"JSESSIONID=XXXXXXXXXXXXXXX"
then using $sent_http_set_cookie variable in access log it will be possible to log only CSESSIONID, even on the client side it will be seen as single header Set-Cookie: CSESSIONID=XXXXXXXXXXXXXXX,JSESSIONID=XXXXXXXXXXXXXXX

I use OpenResty and the question is how I can access response header data and obtain value of second cookie (JSESSIONID in my case)?

I have tried to extract JSESSIONID into resp_header variable using this snippet in my server section, but it's empty. Any ideas are appreciated

    set $resp_header "";
    header_filter_by_lua_block {
      local headers, err = ngx.resp.get_headers()
      if err then
        ngx.log(ngx.ERR, "err: ", err)
        return ngx.exit(500)
      end
      for k, v in pairs(headers) do
        if k:lower() == "set-cookie" then
          local set_cookie = v
          if (type(set_cookie) == "table") then
            for key, value in pairs(set_cookie) do
              if string.match(value:lower(), "jsessionid") then
                val = string.match(value:upper(), "JSESSIONID=[%a%d]+")
                ngx.var.resp_header = val
              end
            end
          end
        end
      end
    }
        location ~ \.php$ {
             root           html;
             fastcgi_pass   unix:///var/run/php-fpm/www.sock;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
             include        fastcgi_params;
    
            header_filter_by_lua_block {
                local cookie = ngx.resp.get_headers()['Set-Cookie'] ---> cookie is a table
                ngx.log(ngx.ERR, table.concat(cookie, ','))
           }
       }
    21 days later

    LubinLew thanks a lot for this suggestion!
    I was able to simplify code to this(the trick was that cookie can be table or a string):

    if headers["Set-Cookie"] ~= nil then 
    
      local set_cookie = headers["Set-Cookie"]
    
      if "table" == type(set_cookie) then
        set_cookie = table.concat(set_cookie, ',')
      end  
      ngx.var.resp_header = set_cookie:upper():match("JSESSIONID=([%w]+)") 
    end
      Write a Reply...