A
A
Arris2021-06-02 15:44:42
Nginx
Arris, 2021-06-02 15:44:42

NGINX: how to set log_not_found value via variable substitution?

I would like to disable `log_not_found` logging for some files. There is no problem if you specify

location = /apple-touch-icon(.*)$ { access_log off; log_not_found off;}

but I don't want to produce locations and try to use map:
map $request_uri $log_not_found_off {
    "/apple-touch-icon.png"                     "off";
    "/apple-touch-icon-precomposed.png"         "off";
    "/favicon.ico"                              "off";
    default                                     "on";
}

Now we are trying to use:
location / {
        log_not_found   "$log_not_found_off";

        try_files   $uri $uri/ /index.php?$query_string;
    }

figurines:
[emerg] invalid value "$log_not_found_off" in "log_not_found" directive, it must be "on" or "off"


Okay, how about if ?

location / {
        if ($log_not_found_off = "off") {
            log_not_found "off";
        }

        try_files   $uri $uri/ /index.php?$query_string;
    }

We get:
[emerg] "log_not_found_off" directive is not allowed here


CHADNT?

PS At the same time, if you pass a similar value for access_log ($access_log_off), it crashes with an error, and if you specify ${access_log_off}, the type config is valid (but still logging).
PPS The comments suggested that it wouldn't work. Okay, let's specify outside the location block:

server {
    listen                  80;
    server_name             foobar.local;

    access_log              /var/log/nginx/main.access.log       combined        ;
    error_log               /var/log/nginx/main.error.log        error           ;

    rewrite_log on;

log_not_found       "$log_not_found_off";

#  ИЛИ

    if ($log_not_found_off = "off") {
        log_not_found $log_not_found_off;
    }
...

The result is similar

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question