【问题标题】:How to debug lua code inside nginx config?如何在 nginx 配置中调试 lua 代码?
【发布时间】:2014-11-29 03:08:16
【问题描述】:

我想在我的 lua 代码中插入日志点(io.write),该代码本身位于 nginx 配置中(使用 HttpLuaModule 用于 nginx)。 怎么做? 访问和错误日​​志没有显示它们。

【问题讨论】:

    标签: debugging nginx lua


    【解决方案1】:

    在 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

    【讨论】:

    • 有没有办法改变日志消息格式?目前我收到一条很长的消息,包括日期、主机等。 "2017/03/03 05:31:22 [] 44#0: *17 [lua] content_by_lua(proj1.conf:110):3: 你的消息在这里,客户端:172.18.0.1,服务器:"
    【解决方案2】:

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

    【讨论】:

    • 谢谢你,保罗我会试试这个方法。我更喜欢这种工作方式,而不是一直打印值。
    【解决方案3】:

    openresty 的另一个故障排除工具是lua-resty-console,它允许您检查 lua 变量、导入的模块、调用函数等。 你所要做的就是使用 luarocks 安装它并在你的 nignx.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" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2016-02-13
      • 2013-09-11
      • 2019-10-29
      • 1970-01-01
      相关资源
      最近更新 更多