Is it possible to set proxy header from Lua code?

2,127 views
Skip to first unread message

Zaar Hai

unread,
May 9, 2019, 2:53:59 AM5/9/19
to openresty-en
Good day guys,

I'm using access_by_lua_block on a proxy server to check incoming credentials and I would like to forward resulting user to the backend. I see that most examples declare nginx var, set it in Lua, then use it in proxy_set_header.

Is there a way to set proxy headers directly from lua?

E.g. instead of:

location / {
    set $user "";
    access_by_lua_block {
        ngx.var.user = "foo"
    }
    proxy_set_header X-Forwarded-User $user;
    proxy_pass ...
}

Something like:
location / {
    access_by_lua_block {
        ngx.proxy.set_header("X-Forwarded-User", "foo")
    }
    proxy_pass ...
}

Thanks in advance,
Zaar

Rob Kay

unread,
May 9, 2019, 10:29:48 PM5/9/19
to openresty-en
I believe you want:

ngx.req.set_header("X-Forwarded-User", "foo")

Zaar Hai

unread,
May 10, 2019, 12:40:20 AM5/10/19
to openresty-en
Thank you! It did the trick.
Reply all
Reply to author
Forward
0 new messages