카테고리 없음
init_by_lua
aucd29
2013. 9. 26. 21:09
phase: loading-config
Runs the Lua code specified by the argument <lua-script-str> on the global Lua VM level
when the Nginx master process (if any) is loading the Nginx config file.
When Nginx receives the HUP signal and starts reloading the config file,
the Lua VM will also be re-created and init_by_lua will run again on the new Lua VM.
Usually you can register (true) Lua global variables
pre-load Lua modules at server start-up by means of this hook.
Here is an example for pre-loading Lua modules:
[code]
init_by_lua 'require "cjson"';
server {
location = /api {
content_by_lua '
ngx.say(cjson.encode({dog = 5, cat = 6}))
';
}
}
[/code]
You can also initialize the lua_shared_dict shm storage at this phase. Here is an example for this:
[code]
lua_shared_dict dogs 1m;
init_by_lua '
local dogs = ngx.shared.dogs;
dogs:set("Tom", 56)
';
server {
location = /api {
content_by_lua '
local dogs = ngx.shared.dogs;
ngx.say(dogs:get("Tom"))
';
}
}
[/code]
Runs the Lua code specified by the argument <lua-script-str> on the global Lua VM level
when the Nginx master process (if any) is loading the Nginx config file.
When Nginx receives the HUP signal and starts reloading the config file,
the Lua VM will also be re-created and init_by_lua will run again on the new Lua VM.
Usually you can register (true) Lua global variables
pre-load Lua modules at server start-up by means of this hook.
Here is an example for pre-loading Lua modules:
[code]
init_by_lua 'require "cjson"';
server {
location = /api {
content_by_lua '
ngx.say(cjson.encode({dog = 5, cat = 6}))
';
}
}
[/code]
You can also initialize the lua_shared_dict shm storage at this phase. Here is an example for this:
[code]
lua_shared_dict dogs 1m;
init_by_lua '
local dogs = ngx.shared.dogs;
dogs:set("Tom", 56)
';
server {
location = /api {
content_by_lua '
local dogs = ngx.shared.dogs;
ngx.say(dogs:get("Tom"))
';
}
}
[/code]