大网数据广告活动
服务器运维问答

Uninitialized variable in access_log

· 52CE 我爱测

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 $…

问题原文

原帖链接 · 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;
    ...
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-09-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。