Skip to content

Latest commit

 

History

History
131 lines (95 loc) · 4.25 KB

log_by_lua.md

File metadata and controls

131 lines (95 loc) · 4.25 KB

log_by_lua

语法: log_by_lua <lua-script-str>

环境: http, server, location, location if

阶段: log

log 请求处理阶段执行内嵌在<lua-script-str>的 Lua 代码。它不替代当前access的日志,而是在其后执行。

注意,当前环境中以下 API 函数当前是被禁用的:

这是一个收集 $upstream_response_time 平均处理的例子:

 lua_shared_dict log_dict 5M;

 server {
     location / {
         proxy_pass http://mybackend;

         log_by_lua '
             local log_dict = ngx.shared.log_dict
             local upstream_time = tonumber(ngx.var.upstream_response_time)

             local sum = log_dict:get("upstream_time-sum") or 0
             sum = sum + upstream_time
             log_dict:set("upstream_time-sum", sum)

             local newval, err = log_dict:incr("upstream_time-nb", 1)
             if not newval and err == "not found" then
                 log_dict:add("upstream_time-nb", 0)
                 log_dict:incr("upstream_time-nb", 1)
             end
         ';
     }

     location = /status {
         content_by_lua '
             local log_dict = ngx.shared.log_dict
             local sum = log_dict:get("upstream_time-sum")
             local nb = log_dict:get("upstream_time-nb")

             if nb and sum then
                 ngx.say("average upstream response time: ", sum / nb,
                         " (", nb, " reqs)")
             else
                 ngx.say("no data yet")
             end
         ';
     }
 }

该指令在v0.5.0rc31版本被首次引入。

返回目录

English source:

log_by_lua

syntax: log_by_lua <lua-script-str>

context: http, server, location, location if

phase: log

Run the Lua source code inlined as the <lua-script-str> at the log request processing phase. This does not replace the current access logs, but runs after.

Note that the following API functions are currently disabled within this context:

Here is an example of gathering average data for $upstream_response_time:

 lua_shared_dict log_dict 5M;

 server {
     location / {
         proxy_pass http://mybackend;

         log_by_lua '
             local log_dict = ngx.shared.log_dict
             local upstream_time = tonumber(ngx.var.upstream_response_time)

             local sum = log_dict:get("upstream_time-sum") or 0
             sum = sum + upstream_time
             log_dict:set("upstream_time-sum", sum)

             local newval, err = log_dict:incr("upstream_time-nb", 1)
             if not newval and err == "not found" then
                 log_dict:add("upstream_time-nb", 0)
                 log_dict:incr("upstream_time-nb", 1)
             end
         ';
     }

     location = /status {
         content_by_lua '
             local log_dict = ngx.shared.log_dict
             local sum = log_dict:get("upstream_time-sum")
             local nb = log_dict:get("upstream_time-nb")

             if nb and sum then
                 ngx.say("average upstream response time: ", sum / nb,
                         " (", nb, " reqs)")
             else
                 ngx.say("no data yet")
             end
         ';
     }
 }

This directive was first introduced in the v0.5.0rc31 release.

Back to TOC