是否可以在openresty中的nginx访问日志中包含lua变量的内容?

问题描述 投票:0回答:1

我需要计算lua块中的一些值,并将结果包含到nginx访问日志中。

像这样: http 块中的某处:

log_format 扩展转义=json .... "timestamp_ns": "$lua_timestamp_ns" ....;

log_by_lua_block 中的某处:

ngx.var.lua_timestamp_ns = nginx.var.msec * 1000

但是,如果我在 log_format 中包含任意变量,我的配置将变得无效:
未知的“lua_timestamp_ns”变量

我尝试使用map或set“声明”变量,但在任何一种情况下,nginx都认为该配置无效。 是否有可能在访问日志中包含一些任意值?

nginx lua nginx-config openresty
1个回答
0
投票

以下配置应该有效:

http {
    log_format extended escape=json 'timestamp_ns: $lua_timestamp_ns';

    server {
        access_log /dev/stdout extended;

        # this line is required to create the variable at config time,
        # see: https://github.com/openresty/lua-nginx-module#ngxvarvariable
        set $lua_timestamp_ns '';

        log_by_lua_block {
            ngx.var.lua_timestamp_ns = ngx.var.msec * 1000
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.