lua-nginx指令的执行
"
- init_by_lua
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.
Usually you can pre-load Lua modules at server start-up by means of this hook and take advantage of modern operating systems’ copy-on-write (COW) optimization.
You can also initialize the lua_shared_dict shm storage at this phase.- init_worker_by_lua
Runs the specified Lua code upon every Nginx worker process’s startup when the master process is enabled.
This hook is often used to create per-worker reoccurring timers (via the ngx.timer.at Lua API), either for backend health-check or other timed routine work.- access_by_lua
Acts as an access phase handler and executes Lua code string specified in lua-script-str for every request. The Lua code may make API calls and is executed as a new spawned coroutine in an independent global environment (i.e. a sandbox)- balancer_by_lua_block
This directive runs Lua code as an upstream balancer for any upstream entities defined by the upstream {} configuration block.- header_filter_by_lua
Uses Lua code specified in lua-script-str to define an output header filter.- body_filter_by_lua
Uses Lua code specified in lua-script-str to define an output body filter.
初始化
初始化全局环境变量(init_by_lua*)
1 | init_by_lua_block { |
1 | Wukong.init(global_conf_path) |
初始化任务(init_worker_by_lua*)
1 | init_worker_by_lua_block { |
1 | Wukong.initWorker //初始化事件注册中心,定时器加载配置信息和插件的 |
Timer.at的回调方法可能由于代码错误在系统中累积,耗尽系统资源
(原版解释:Because timer callbacks run in the background and their running time will not add to any client request’s response time, they can easily accumulate in the server and exhaust system resources due to either Lua programming mistakes or just too much client traffic)
Timer.every 版本要求:v0.10.9;openresty-1.11.2.5.tar.gz 及以上版本支持nginx.every
请求处理
access(access_by_lua*)
1 | access_by_lua_block { |
1 | Wukong.access() |
balance(balance_by_lua*)
1 | upstream default_upstream { |
1 | Wukong.balancer() |
- balancer方法执行的次数
balancer的执行在不超过阀值时,直至成功为止