1

I would like to disable access logging for some specific paths but still proxy it to another container. In other words "match multiple locations without returning/exiting" which is not possible as far as I know.

The following config will make nginx cancel the request without entering the proxy pass location.

server {
    # ...

    # do not log requests for /_nuxt/* and /_ipx/*
    location ~ ^/(_ipx|_nuxt) {
        access_log off;
    }

    # still proxy these paths
    location ~* ^(\/|\/(foo|bar|_nuxt|_ipx)$ {
        proxy_pass         http://frontend:3000;
        proxy_http_version 1.1;
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $server_name:$server_port;
    }
}

Is there a more clean way of achieving the desired behavior other than duplicating the proxy configuration and adding the access log config line to that second location?

server {
    # ...

    # proxy pass without _nuxt and _ipx
    location ~* ^(\/|\/(foo|bar)$ {
        proxy_pass         http://frontend:3000;
        proxy_http_version 1.1;
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $server_name:$server_port;
    }

    # access log + proxy pass
    location ~ ^/(_ipx|_nuxt) {
        access_log off;

        proxy_pass         http://frontend:3000;
        proxy_http_version 1.1;
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $server_name:$server_port;
    }
}

romanzipp
  • 1,578
  • 3
  • 10
  • 13

2 Answers2

1

You're right, location working like switch case taking the first hit and break. Maybe you can try something like that:

if ($request_uri ~ ^/(_ipx|_nuxt)) {
    access_log off;
  }

instead of your first location statement.

araisch
  • 1,727
  • 4
  • 15
  • It may work for the case in the original question, but this [isn't recommended](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/) :). Please see [the other answer](https://stackoverflow.com/a/70741432/761202) for a supported/recommended approach. – AD7six Jan 17 '23 at 08:53
  • Directive if has problems **when used in location context**. Maybe you have another approach, but if is not bad in general.. – araisch Jan 17 '23 at 15:54
  • Sorry, was your answer using it in a server block? Assuming so, that wasn't clear to me. I do have another approach - see the other answer :). I disagree `if` is not bad in general - it _is_ best avoided, please see the linked reference (which I recognise you're quoting from in your comment) - alternative argument: there is a built in way to do what was asked, `if` is not necessary. – AD7six Jan 17 '23 at 18:10
  • Yes, my statement is instead of the first location, therefore in the server block. If you read the linked reference, you will realize that if is just evil if you use it as conditional routing. I use it in a totally different context. So your solution is not better imho, just different. – araisch Jan 19 '23 at 08:38
1

The access_log directive has the following syntax:

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; ...

...

The if parameter (1.7.0) enables conditional logging. A request will not be logged if the condition evaluates to “0” or an empty string. In the following example, the requests with response codes 2xx and 3xx will not be logged:

map $status $loggable {
    ~^[23]  0;
    default 1; 
}

access_log /path/to/access.log combined if=$loggable;

Applied to the asked question, that means the following config should achieve the desired goal:

map $uri $loggable {
    ~^/_(ips|nuxt)    0;
    default           1;
}
server {
    ...
    access_log /path/to/access.log <format> if=$loggable;
}
AD7six
  • 63,116
  • 12
  • 91
  • 123
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
  • With an edit (that I'll make, because most of the work is in this answer already) this is actually a more appropriate solution than the accepted answer - as recommended by [the official nginx docs](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/#enabling-conditional-logging) – AD7six Jan 17 '23 at 08:32