skip to Main Content

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

2

Answers


  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.

    Login or Signup to reply.
  2. 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;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search