Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx:在多个条件下登录_Nginx_Logging_Conditional - Fatal编程技术网

Nginx:在多个条件下登录

Nginx:在多个条件下登录,nginx,logging,conditional,Nginx,Logging,Conditional,我正在尝试配置nginx/1.13.0和条件访问日志 如果access_日志记录仅以$status代码为条件,则一切正常: http { [..] map $status $logworthy_status { ~^[4] 1; default 0; } [..] server { [..] access_log /var/log/nginx_access.log c

我正在尝试配置nginx/1.13.0和条件访问日志

如果access_日志记录仅以$status代码为条件,则一切正常:

http {
     [..]
     map $status $logworthy_status {
         ~^[4]  1;
         default 0;
     }
     [..]
     server {
          [..]
          access_log  /var/log/nginx_access.log combined if=$logworthy_status;
          [..]
     }
}
调试日志显示映射按预期运行:

2017/06/13 11:34:14 [debug] 23153#0: *203 http map started
2017/06/13 11:34:14 [debug] 23153#0: *203 http script var: "401"
2017/06/13 11:34:14 [debug] 23153#0: *203 http map: "401" "1"
但是,如果我试图重写它以允许建议的多个条件:

不会生成任何日志消息。检查调试日志显示,即使$status上的映射也似乎有问题:

2017/06/13 11:38:12 [debug] 23631#0: *204 rewrite phase: 0
2017/06/13 11:38:12 [debug] 23631#0: *204 http script value: "0"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script set $logworthy
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var
2017/06/13 11:38:12 [debug] 23631#0: *204 http map started
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var: "000"
2017/06/13 11:38:12 [debug] 23631#0: *204 http map: "000" "0"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var: "0"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script value: "1"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script equal
2017/06/13 11:38:12 [debug] 23631#0: *204 http script equal: no
2017/06/13 11:38:12 [debug] 23631#0: *204 http script if
2017/06/13 11:38:12 [debug] 23631#0: *204 http script if: false
有人能解释一下吗?请求按预期得到处理,nginx返回401,但它不会记录此情况。

您可以使用多个指令,并使用上一个块中的默认值。例如,在用户代理不包含“AppleWebKit”的情况下记录4xx状态请求

2017/06/13 11:38:12 [debug] 23631#0: *204 rewrite phase: 0
2017/06/13 11:38:12 [debug] 23631#0: *204 http script value: "0"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script set $logworthy
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var
2017/06/13 11:38:12 [debug] 23631#0: *204 http map started
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var: "000"
2017/06/13 11:38:12 [debug] 23631#0: *204 http map: "000" "0"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var: "0"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script value: "1"
2017/06/13 11:38:12 [debug] 23631#0: *204 http script equal
2017/06/13 11:38:12 [debug] 23631#0: *204 http script equal: no
2017/06/13 11:38:12 [debug] 23631#0: *204 http script if
2017/06/13 11:38:12 [debug] 23631#0: *204 http script if: false
map $status $loggable_status {
    ~^[4]   1;
    default 0;
}
map $http_user_agent $loggable_user_agent {
    ~*AppleWebKit   0;
    default $loggable_status; # Use map var above as default
}

access_log /var/log/nginx/error.log combined if=$loggable_user_agent;