Home > Blockchain >  nginx: Match multiple locations / disable access log without returning
nginx: Match multiple locations / disable access log without returning

Time:01-18

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;
    }
}

CodePudding user response:

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.

CodePudding user response:

Update: the following solution is not workable. I leave it as a warning to everyone. While the same approach is workable with some other directives (e.g. auth_basic, working example), it is unworkable with the access_log directive.


Solution proposed by @araisch should work, just for completeness there is one more option to do the same using the map block:

map $uri $logfile {
    ~^/_(ips|nuxt)    off;
    default           /path/to/access.log;
}
server {
    ...
    access_log $logfile; # or 'access_log $logfile <format>'
    ...
}
  •  Tags:  
  • Related