全局 Lua 模块
全局 Lua 模块提供了一种强大的机制,用于实现复杂逻辑功能。
与 YAML 配置文件不同,全局 Lua 模块以 .lua
作为文件扩展名,并且必须存放在配置目录中名为 global_lua_modules
的子目录下。
示例
创建一个名为 hello.lua
的 Lua 模块:
local _M = {}
function _M.go()
ngx.say("hello")
end
return _M
在页面规则中调用 hello
Lua 模块:
- enable_rule: true
actions:
"user-code":
el: |-
{
true =>
foreign-call(module: "hello", func: "go"),
done;
}
comment: ''
在上述示例中,定义了一个简单的 Lua 模块 hello.lua
,该模块包含了一个名为 go
的函数,用于输出 “hello”。然后,在页面规则的配置中,通过表达式调用了这个模块和函数。