Opened 2 years ago
Closed 2 years ago
#2414 closed enhancement (wontfix)
support unknown variables when it is only used in log format
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | minor | Milestone: | |
Component: | nginx-module | Version: | 1.21.x |
Keywords: | Cc: | ||
uname -a: | Linux cent82-2c4g 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux | ||
nginx -V: |
nginx version: nginx/1.21.4
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) built with OpenSSL 1.1.1o 3 May 2022 TLS SNI support enabled configure arguments: --prefix=/home/ngx/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_sub_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_addition_module --with-openssl=/home/ngx/build_ngx/openssl-1.1.1o --with-zlib=/home/ngx/build_ngx/zlib-1.2.11 --with-pcre=/home/ngx/build_ngx/pcre-8.45 |
Description
currently, when we add some extend variable in log format which is not yet defined with "set" or other command, it will raise emerg error: unknown "xxxxxx" variable. so if you want change log field, depend on other config, server block, eg.
sometimes it is needed to extend variables in logs in batch, and the variable, is set only in some instance, and is not set in other instance.
it can be as $http_ or $arg_, even $http_xxxx variable is not found in head, it will not raising error when writing log, and not raise error when do test or reload.
for every variable, of only used in log format, just not raise error. or for specify variables, add similar way: $ngx_ext_ , eg.
Change History (4)
follow-up: 2 comment:1 by , 2 years ago
comment:2 by , 2 years ago
Replying to Maxim Dounin:
Note that
$http_*
,$arg_*
and some other variables are special, as these variables lookup particular headers (or arguments) in the request. Since it is not possible to tell during a configuration parsing if a particular header will be available or not, these variables are always available.
For normal variables nginx knows if these are defined somewhere in the configuration or not, and can reject obviously invalid cases during configuration parsing. And this is what it does.
If for some reason you want to use a variable which is known to be never set in nginx configuration, consider providing a dummy set somewhere, so nginx won't complain. For example,
set
in an internal location which cannot be reached might be a good solution.
There are many nginx instances used by different user.
And they have same log format, mantained by manager, if manager want add field, need inform users to add "set" first, it is not easy.
so, in this scenario, it is better to support unknown variable or a similar as $http_ in log format.
comment:3 by , 2 years ago
As long as the log format is maintained somehow, there should be a way to maintain appropriate set
directives as well.
Another (and probably less intrusive that using set
) approach to define variables would be to provide some regular expression with appropriate variables. For example, at the same level as log_format
this can be done with a map
:
map dummy $dummy { ~(?<foo>)(?<bar>)(?<bazz>) dummy; } log_format test "$foo $bar $bazz";
While we can consider a more natural way to define variables, I don't think that allowing arbitrary unknown variables is a way to go.
comment:4 by , 2 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
As per previous comments, closing this.
Note that
$http_*
,$arg_*
and some other variables are special, as these variables lookup particular headers (or arguments) in the request. Since it is not possible to tell during a configuration parsing if a particular header will be available or not, these variables are always available.For normal variables nginx knows if these are defined somewhere in the configuration or not, and can reject obviously invalid cases during configuration parsing. And this is what it does.
If for some reason you want to use a variable which is known to be never set in nginx configuration, consider providing a dummy set somewhere, so nginx won't complain. For example,
set
in an internal location which cannot be reached might be a good solution.