nginxluanginx-reverse-proxynginx-locationopenresty

How to access a variable value set inside Lua block in proxy pass


I am new to nginx. I have a variable set $myuser and updating the value ngx.var.myuser inside rewrite_by_lua. I want to pass this as a header in proxy_pass. I am using proxy_add_header to add $myuser but I am not getting the updated value.

code block:

location /
{

 set $myuser '';

    rewrite_by_lua_block
    {
        local user=//code to get the updated value
       //perform some redirection logic on error
        ngx.var.myuser= user
       
       
    }
  
proxy_pass  "https://backend.com"
proxy_set_header  myheader  $myuser


}

value of $myuser is coming as empty but it's getting retrieved fine inside the lua block.


Solution

  • Below code worked. No need to set variable anduse it in proxy_set_header. We can set the header inside the lua block

    rewrite_by_lua_block
        {
            local user=//code to get the updated value
           //perform some redirection logic on error
           
             ngx.req.set_header("myheader", user)
           
           
        }