Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exchanging variables between LUA directives in NGINX

Tags:

nginx

lua

I am using HttpLuaModule for my NGINX server. I want to ask if it is possible to recognize variable in different directives of this module. For example

 init_by_lua ' local global_var = 5 ' ;
 some config ;
 set_by_lua ' print(global_var) ' ;

How is that possible with NGINX and LUA module ?

like image 590
HobbitOfShire Avatar asked May 24 '26 09:05

HobbitOfShire


1 Answers

If you want global_var to be global, don't declare it as local.

From a quick look at NGINX's docs, init_by_lua and set_by_lua work on the same global Lua state and so you'll be able to make them talk if you use global variables. Local variables set in init_by_lua will be lost.

So, it should work if you just remove local in init_by_lua.

like image 65
lhf Avatar answered May 27 '26 06:05

lhf