问题原文
原帖链接 · Kevin Zarca · CC BY-SA 4.0
I have the following config file:
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;
...
I often get a warning similar to this one:
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"
As I set $logging 1 in the same context as access_log, how could it be uninitialized?
已采纳解答
原帖链接 · Richard Smith · CC BY-SA 4.0
I suspect that Nginx evaluates the access_log directive in an earlier phase than those of the rewrite module (i.e. set, if, rewrite and return).
You may be able to eliminate the set and if statements by using a different default clause in the second map block.
For example:
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;
...
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
