我的字符串如下所示:nxs_dev_flo.nexus
我只想返回 nxs_dev_flo
这是我尝试过的:
location /luatest {
default_type 'text/plain';
content_by_lua 'ngx.say(split(ngx.var.host, "."))';
}
我在日志中收到 500 错误:
2018/02/06 17:59:52 [error] 7237#7237: *87 lua entry thread aborted: runtime error: content_by_lua(default:55):1: attempt to call global 'split' (a nil value) stack traceback: coroutine 0: content_by_lua(default:55): in function , client: 127.0.0.1, server: _, request: "GET /luatest HTTP/1.1", host: "nxs_flo_dev.nexus"
最佳答案
split
不是标准的 Lua 函数,显然也不是 nginx 提供的函数。
尝试使用 ngx.say(ngx.var.host:match("(.-)%."))
代替。
string.match是一个标准的 Lua 函数。
关于linux - 用Lua分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648225/