首页 > 解决方案 > Nginx - 在 access.log 中记录一个可能未初始化的变量

问题描述

我的设置如下:

位置.conf

location /foo {
    set $x "foo"
}

location /foo2 {
    set $notX "foo2"
}

nginx.conf

http {
..
..
log_format access_format $x
access_log  /var/log/nginx/access.log  access_format ;
..
..
}

当我点击 /foo 时,访问日志会正确打印,但是当我访问 /foo2 时,日志会打印在 error.log 中,但有异常 using uninitialized "x" variable while logging request

我尝试在其中添加 if 条件location.confnginx.conf但这会导致配置无效。我还尝试使用nginx.conf下面的 map 来默认值,但这也无济于事

http {
..
..
map $x $new_x {
  '' "dummy"
  default $x
}

log_format access_format $new_x
access_log  /var/log/nginx/access.log  access_format ;
..
..
}

标签: nginxloggingnginx-confignginx-locationnginx-log

解决方案


推荐阅读