我有以下配置文件:
geo $log_ip {
X.X.X.X 0;
default 1;
}
map $request_uri $loggable {
~/app 0;
default 1;
}
server {
server_name www.example.com
listen 80;
listen [::]:80;
set $logging 1;
if ( $log_ip = 0 ) {
set $logging 0;
}
if ( $loggable = 0 ) {
set $logging 0;
}
access_log /var/log/nginx/access.log combined if=$logging;
...
我经常会收到类似这样的警告:
2020/09/23 08:29:10 [warn] 8#8: *364785 using uninitialized "logging" variable while logging request, client: Y.Y.Y.Y, server: www.example.com, request: "anything", host: "X.X.X.X"
由于我在与 access_log 相同的上下文中设置了 $logging 1,它怎么会未初始化呢?
答案1
我怀疑 Nginx在比重写模块(即 、 和)更早access_log
的阶段评估该指令。set
if
rewrite
return
您可能能够通过在第二个块中使用不同的子句来消除set
and语句。if
default
map
例如:
geo $log_ip {
X.X.X.X 0;
default 1;
}
map $request_uri $loggable {
~/app 0;
default $log_ip;
}
server {
...
access_log /var/log/nginx/access.log combined if=$loggable;
...
}