Search code examples
nginxluanginx-reverse-proxyopenresty

openResty fails to write log header


I have the following configuration to write logs to access.log

START Header logging with LUA with Masking

    set $resp_header "";
    header_filter_by_lua_block {
        local rh = ngx.resp.get_headers()
        for k, v in pairs(rh) do
            ngx.var.resp_header = ngx.var.resp_header .. k.."="..v.." "
        end}

END Header logging with LUA with Masking

This works fine for most of my upstream responses. but for the one that sends duplicate header of the same name "Set-Cookie" and both are pretty lengthy in number of characters. nginx error log says .

[error] 3142684#3142684: 790582 failed to run header_filter_by_lua: header_filter_by_lua:11: attempt to concatenate local 'v' (a table value)

I am looking into all possible LUA documentation. Any help will be appreciated.

As a workaround I have set the header "Set-Cookie" forcefully in nginx.

#--- for k, v in pairs(rh) do if k:lower() == ("set-cookie") then v="REDACTED" end #---

Thanks.


Solution

  • Some of the headers are allowed to repeat (Set-Cookie is one of them) and will be returned as a table by get_headers() function, which your code doesn't take into account (hence the error you're getting).

    Replace ngx.var.resp_header .. k.."="..v.." " with something like ngx.var.resp_header .. k.."="..(type(v) == "table" and table.concat(v, ";") or v).." "