Access ngx.var.Variable outside Lua block

220 views
Skip to first unread message

DevX

unread,
Jul 22, 2019, 8:03:46 AM7/22/19
to openresty-en
Hi,

I'm setting the ngx variable inside content by lua block but the updated value is not available outside.


set $varEx ' '
content_by_lua_block {
ngx.var.varEx = "someNewValue"
}
proxy_set_header $xxx $varEx
proxy_pass


I want to use the updated value to pass as header before proxy_pass. 
Same is working with capture, but the api response is large. So have to avoid capture.
Please suggest.

Thanks.


DevX

unread,
Jul 22, 2019, 8:23:11 AM7/22/19
to openresty-en
Any other way to access variables across with good performance would be of great help.

Michael Noe

unread,
Jul 22, 2019, 8:51:40 AM7/22/19
to openre...@googlegroups.com
Is it due to the missing semicolons?  I’m doing the same thing with content_by_lua_file and it definitely works.


--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openresty-en/045efcb4-abc8-4096-909c-d235587a1031%40googlegroups.com.

Gene Unigovski

unread,
Jul 22, 2019, 9:44:43 AM7/22/19
to openresty-en
...use set_by_lua_block or access_by_lua_block - proxy_pass and content_by_lua_block are on the same req processing step...

...you cam also use ngx.req.set_header() instead of proxy_set_header - it would make code cleaner... 

DevX

unread,
Jul 23, 2019, 1:58:54 AM7/23/19
to openresty-en
Thanks for the info. 

We can set header using ngx.req.set_header() and ngx.var.http_headerName . Which one is better ?

Gene Unigovski

unread,
Jul 23, 2019, 8:40:58 AM7/23/19
to openresty-en
...these two are different ...I would suggest to use ngx.req.set_header() - especially in case if you are planning to proxy_pass request to upstream... 

DevX

unread,
Jul 23, 2019, 9:50:46 AM7/23/19
to openresty-en
Cool thanks.
Reply all
Reply to author
Forward
0 new messages