Opened 13 months ago

Last modified 13 months ago

#2475 new defect

access_log with if does not work when variable name starts with a number

Reported by: taoky@… Owned by:
Priority: minor Milestone:
Component: nginx-core Version: 1.19.x
Keywords: Cc:
uname -a: Linux hostname 6.1.12-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 14 Feb 2023 22:08:08 +0000 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.22.1
built with OpenSSL 3.0.7 1 Nov 2022 (running with OpenSSL 3.0.8 7 Feb 2023)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/bin/nginx --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock --user=http --group=http --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --http-client-body-temp-path=/var/lib/nginx/client-body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-cc-opt='-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto' --with-ld-opt='-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto' --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-pcre-jit --with-stream --with-stream_geoip_module --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads

Description (last modified by taoky@…)

The nginx.conf for reproducing looks like this:

error_log /dev/stdout info;
pid /tmp/nginx/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    client_body_temp_path /tmp/nginx/;
    fastcgi_temp_path /tmp/nginx/fastcgi/;
    uwsgi_temp_path /tmp/nginx/uwsgi/;
    scgi_temp_path /tmp/nginx/scgi/;
    server {
        listen 8000 default_server;
        listen [::]:8000 default_server ipv6only=on;
        root /tmp/;
        server_name localhost;
        client_max_body_size 100m;
        set $123_test 0;
        access_log /tmp/access.log combined if=$123_test;
        #set $test_123 0;
        #access_log /tmp/access.log combined if=$test_123;
    }
}

When $123_test is set to 0 in the server block, it will still write to /tmp/access.log as if it does not evaluate to "0". However, if changing its name to $test_123, nothing will be logged by access_log.

It seems that the if in access_log only looks at the first character in this case. When the variable's name is changed to $0123_test, it will also log nothing, no matter its evaluation result.

I have read the documentation of ngx_http_log_module and I think that it looks like a bug, as variables starts with digits work well other places, such as in if block.

Change History (2)

comment:1 by taoky@…, 13 months ago

Description: modified (diff)

comment:2 by Roman Arutyunyan, 13 months ago

$1...$9 variable names are reserved for regex captures. The access_log directive in your example treats its if= argument as $1 followed by a literal 23_test, which is why the result is neither zero, not empty. Use can use braces to avoid such issues: ${123_test}.

Note: See TracTickets for help on using tickets.