【问题标题】:How to write only logs with 200 status如何只写入状态为 200 的日志
【发布时间】:2013-10-01 10:42:55
【问题描述】:

我正在尝试弄清楚如何执行以下操作:

  1. 请求即将到来。

  2. HttpLuaModule 对请求执行一些操作。如果请求有效,Lua 将使用ngx.exit(202) 完成处理。但是在处理过程中可能(并且将会)发生一些情况,nginx 可能会返回 403、404、503 错误。

我想要做的是仅写入访问日志具有 200 状态代码的请求。 基本上我想做这样的事情:

location /foo {
    content_by_lua_file "/opt/nginx/lua/process.lua";
    if (status == 200) {
        access_log "/path/to/the/access_log"
    } 

我对 nginx 和 lua 都很陌生,所以对我来说,找出放置位置和 if 语句(content_by_lua_file 之后的 ether 或在侧 lua 文件中)以及这个 if 语句应该是什么样子是一个挑战喜欢。

【问题讨论】:

    标签: nginx lua http-status-codes


    【解决方案1】:

    nginx 1.7.0+ 允许在 access_log 指令本身中使用 if 条件。

    access_log path [format [buffer=size [flush=time]] [if=condition]];
    
    The if parameter (1.7.0) enables conditional logging.
    A request will not be logged if the condition evaluates to “0” or an empty string
    

    结合map 指令,可以根据各种条件将日志事件发送到不同的日志。

    http {
    
        map $status $normal {
            ~^2  1;
            default 0;
        }
        map $status $abnormal {
            ~^2  0;
            default 1;
        }
        map $remote_addr $islocal {
            ~^127  1;
            default 0;
        }
    
        server {
    
            access_log logs/access.log combined if=$normal;
            access_log logs/access_abnormal.log combined if=$abnormal;
            access_log logs/access_local.log combined if=$islocal;
    
        }  
    }
    

    http://nginx.org/en/docs/http/ngx_http_log_module.html
    http://nginx.org/en/docs/http/ngx_http_map_module.html

    【讨论】:

    • 这太棒了。它回答了我关于条件日志的一个有点不同的问题。
    【解决方案2】:

    您可以使用ngx.loglog_by_lua 指令来实现。

    location /conditional_log{
            log_by_lua 'if ngx.status == 200 then ngx.log(ngx.ERR, "It is 200") end';
            content_by_lua 'ngx.say("I am ok") ngx.exit(200)';
        }
    

    在上面的代码中,我们使用了log_by_lua,它在日志阶段运行时被调用。如果ngx.status == 200,我们使用ngx.log 来触发使用ngx.log 的日志记录。

    这将写入error_log。不知道怎么写到access_log

    供参考

    http://wiki.nginx.org/HttpLuaModule#ngx.log

    http://wiki.nginx.org/HttpLuaModule#log_by_lua

    【讨论】:

    • 谢谢你的回答,但我真的很想写到access_log,我想出了一个非常奇怪的解决方案:设置一个全局变量foo然后将content_by_lua更改为rewrite_by_lua和最后,如果一切正常,我将变量 foo 更改为 200。然后在脚本之后,我有一个类似的 if 语句,我在问题中发布。希望有人能提出更优雅的建议。
    【解决方案3】:

    每个问题都是答案的一部分。你很亲密:

    if ($status != "200") {
        access_log off;
    }
    

    在此处查看版本可用性信息。 http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

    此外,几乎所有访问日志格式变量都可以在“现代”版本中使用: http://nginx.org/en/docs/http/ngx_http_log_module.html

    【讨论】:

      【解决方案4】:

      这是我想出的解决方案:

      auth.lua

      -- Some logic goes here
      -- ....
      -- ....
      ngx.var.return_status = 200
      

      nginx.conf

      http {
         lua_package_path .....;
         lua_package_cpath ....;
      
         rewrite_by_lua_no_postpone on;
      
         server {
            
           set $return_status 1;
          
           location /foo {
              rewrite_by_lua_file "<apth_to_aut.lua";
      
              if ($return_status = 200) {
                  access_log  <path_to_access_log>  format;
                  return 200;
              }
           }
         }  
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-02
        • 2011-08-24
        • 1970-01-01
        • 2021-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-04
        相关资源
        最近更新 更多