Lua 中的字符串分割

我的字符串长这样: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"

点赞
用户107090
用户107090

split 不是标准的 Lua 函数,并且显然不是 nginx 提供的函数。

请尝试使用 ngx.say(ngx.var.host:match("(.-)%."))

string.match 是标准的 Lua 函数。

`split` is not a standard Lua function and apparently not one provided by nginx.

Try `ngx.say(ngx.var.host:match("(.-)%."))` instead.

[string.match](https://www.lua.org/manual/5.3/manual.html#pdf-string.match) is a standard Lua function.
2018-02-06 17:25:15