如何在nginx配置文件中调试lua代码?

我想在我的 Lua 代码中插入日志点(使用 io.write),这些 Lua 代码是在 nginx 配置中使用 HttpLuaModule 进行的。 该怎么做? 访问日志和错误日志并没有显示它们。

点赞
用户3176851
用户3176851

在使用 nginx 时,应该使用 ngx.log。例如:

ngx.log(ngx.STDERR, 'your message here')

有一个可行的示例,请参见http://linuxfiddle.net/f/77630edc-b851-487c-b2c8-aa6c9b858ebb

有关文档,请参见http://wiki.nginx.org/HttpLuaModule#ngx.log

2014-10-04 14:38:02
用户1442917
用户1442917

另一个在nginx中进行Lua调试的选择(除了“打印”)是使用支持远程调试的Lua IDE。我在这里发布了如何使用ZeroBrane Studio IDE进行此操作的说明。一旦设置完成,您将获得大多数您期望的调试功能,如逐步执行、断点、变量检查、堆栈跟踪以及一个运行Lua命令的控制台,可以在远程环境下运行。

2015-07-11 05:30:04
用户590307
用户590307

另一个用于 OpenResty 的故障排查工具是lua-resty-console,它允许您检查 Lua 变量、导入模块、调用函数等。您要做的就是使用luarocks安装它,并在您的nginx.conf中插入几行代码。启动客户端,您就可以使用它了。

[9] ngx(content)> ngx.config.prefix()
=> /workspace/lua-resty-console/
[10] ngx(content)> ngx.config.ngx_lua_version
=> 10011
[11] ngx(content)> ngx.config.nginx_configure()
=>  --prefix=/usr/local/Cellar/openresty/1.13.6.1/nginx --with-cc-opt='-O2 -I/usr/local/include -I/usr/local/opt/pcre/include -I/usr/local/opt/openresty-openssl/include' --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 ...

[12] ngx(content)> ngx.sha →→
ngx.sha1_bin()  ngx.shared.
[12] ngx(content)> ngx.shared. →→
ngx.shared.mycache.    ngx.shared.metrics.
[12] ngx(content)> c = ngx.shared.mycache
=> nil
[13] ngx(content)> c
=> { <userdata 1>,
  <metatable> = <1>{
    __index = <table 1>,
    add = <function 1>,
    delete = <function 2>,
    flush_all = <function 3>,
    flush_expired = <function 4>,
    get = <function 5>,
    get_keys = <function 6>,
    get_stale = <function 7>,
    incr = <function 8>,
    llen = <function 9>,
    lpop = <function 10>,
    lpush = <function 11>,
    replace = <function 12>,
    rpop = <function 13>,
    rpush = <function 14>,
    safe_add = <function 15>,
    safe_set = <function 16>,
    set = <function 17>
  }
}
[14] ngx(content)> c:set('a', 1)
=> true
[15] ngx(content)> c:get('a')
=> 1
[16] ngx(content)> c:get_keys()
=> { "a" }
2019-01-28 03:30:56