Score:0

Nginx, exclude specific URL for access log

ar flag

I need to exclude from logging a specific URL.

I'm getting in my logs a lot of this:

2022-11-10T01:41:14.909240104Z  XX.XX.XX.XX - - [10/Nov/2022:01:41:14 +0000] "GET /myapp/readiness HTTP/1.1" 200 4771 "-" "EF-xx-HealthCheck-Client/1.0" "XX.XX.XX.XX"
2022-11-10T01:41:17.002141788Z  XX.XX.XX.XX - - [10/Nov/2022:01:41:17 +0000] "GET /myapp/readiness HTTP/1.1" 200 4771 "-" "EF-xx-HealthCheck-Client/1.0" "XX.XX.XX.XX"

I tried this at the end of many location blocks:

location ~ ^/myapp/readiness {
    access_log off;
    log_not_found off;
}

This was taken from:

How to turn off access log for only a certain url in this nginx configuration?

But it doesn't work, that's why I ask again.

What else can I do?

Edit:

Full nginx.conf

server {
    listen       8080;
    server_name  localhost;

    root /usr/share/nginx/html;
    index /myapp/index.html;

    ssl_protocols TLSv1.2;

    gzip on;
    gzip_min_length 1000;
    gzip_comp_level 9;
    gzip_buffers 16 8k;
    gzip_http_version 1.0;
    gzip_proxied any;

    gzip_types
      text/css
      text/plain
      text/javascript
      application/javascript
      application/json
      application/x-javascript
      application/xml
      application/xml+rss
      application/xhtml+xml
      application/x-font-ttf
      application/x-font-opentype
      application/vnd.ms-fontobject
      image/svg+xml
      image/x-icon
      application/rss+xml
      application/atom_xml;

    gzip_vary on;
    gunzip on;

    location / {
      return 301 myapp/;
    }

    location ~ ^/myapp/mio-bff {
      rewrite ^/myapp/mio-bff(.*)$ $1 break;
      proxy_pass http://mio-bff.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/myapp-admin-web {
      rewrite ^/myapp/myapp-admin-web(.*)$ $1/ break;
      proxy_pass http://myapp-admin-web.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/mio-reports-bff {
      rewrite ^/myapp/mio-reports-bff(.*)$ $1/ break;
      proxy_pass http://mio-reports.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/notifications-bff {
      rewrite ^/myapp/notifications-bff(.*)$ $1/ break;
      proxy_pass http://notifications-bff.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/compensacion-bff {
      rewrite ^/myapp/compensacion-bff(.*)$ $1/ break;
      proxy_pass http://compensacion-bff.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/mievolucion-bff {
      rewrite ^/myapp/mievolucion-bff(.*)$ $1/ break;
      proxy_pass http://mievolucion-bff.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/requests-bff {
      rewrite ^/myapp/requests-bff(.*)$ $1/ break;
      proxy_pass http://requests-bff.cl-myapp:80;
      proxy_redirect off;
    }

    location ~ ^/myapp/credentials-bff {
      rewrite ^/myapp/credentials-bff(.*?)/?$ $1/ break;
      proxy_pass http://credentials-web-bff.cl-myapp:5000;
      proxy_redirect off;
    }

    location ~ ^/myapp/containerstatics {
      rewrite ^/myapp/containerstatics/(.*)$ /statics/$1 break;

      proxy_pass https://XXX.blob.core.windows.net;
      proxy_redirect off;
    }

    location ~ ^/myapp/containerdynamics {
      rewrite ^/myapp/containerdynamics/(.*)$ /dynamics/$1 break;

      proxy_pass https://XXX.blob.core.windows.net;
      proxy_redirect off;
    }

    location ~ ^/myapp/containerdocuments {
      rewrite ^/myapp/containerdocuments/(.*)$ /documents/$1 break;

      proxy_pass https://XXX.blob.core.windows.net;
      proxy_redirect off;
    }

    location ~ ^/myapp/containerdocsprod {
      rewrite ^/myapp/containerdocsprod/(.*)$ /myappdocsprod/$1 break;

      proxy_pass https://XXX.blob.core.windows.net;
      proxy_redirect off;
    }

    location ~* \.(?:css|js)$ {
      try_files $uri =404;
      access_log off;
      expires 30d;
      add_header Cache-Control public;
      tcp_nodelay off;
      open_file_cache max=3000 inactive=120s;
      open_file_cache_valid 45s;
      open_file_cache_min_uses 2;
      open_file_cache_errors off;
    }

    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff)$ {
      try_files $uri =404;
      expires 365d;
      access_log off;
      add_header Vary Accept-Encoding;
    }

    location ~ ^/myapp {
      try_files $uri /myapp/index.html;
    }

    # Turn off loggin on readiness health check
    location /myapp/readiness {
        access_log off;
        log_not_found off;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # Include MIME Types
    include /etc/nginx/mime.types;

    # Do not verify client ssl
    ssl_verify_client off;
}

EDIT 2:

I tried by changing:

location ~ ^/myapp {
  # index index.html;
  try_files $uri /myapp/index.html;
}

location = /myapp/readiness {
    access_log off;
    log_not_found off;
}

With:

location ^~ /myapp/readiness {
    access_log off;
    log_not_found_off;
    try_files $uri /myapp/index.html;
}

But I'm getting this loop:

XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/readiness HTTP/1.1" 301 170 "-" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/readiness HTTP/1.1" 301 170 "-" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/readiness" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/readiness" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
XX.XX.XX.XX - - [10/Nov/2022:19:54:34 +0000] "GET /myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/ HTTP/1.1" 301 170 "http://NN.NN.NN.NN:8080/myapp/myapp/myapp/myapp/myapp/myapp/myapp/myapp/" "kube-probe/1.21" "-"
us flag
Please share your full nginx configuration as shown by `nginx -T`.
pmiranda avatar
ar flag
@TeroKilkanen done
Score:0
us flag

The reason is the following combination of blocks:

location ~ ^/myapp {
    try_files $uri /myapp/index.html;
}

location /myapp/readiness {
    access_log off;
    log_not_found off;
}

As nginx documentation explains, nginx uses the following method to evaluate location blocks:

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used.

In your case, the following happens when request path is /myapp/readiness:

  1. nginx sees a match to /myapp/readiness location block, and remembers it.
  2. nginx looks through regular expressions, and sees that the request matches ~ ^/myapp location. Therefore it uses that block for handling request.

To prevent nginx to look through the regular expression location blocks, you can use the following definition:

location ^~ /myapp/readiness {
    access_log off;
    log_not_found_off;
    try_files $uri /myapp/index.html;
}

You need to copy the try_files statement from another block here, because otherwise nginx would use the default try_files.

As a sidenote, your configuration has many regular expression based location blocks, although normal prefix matching would be enough.

pmiranda avatar
ar flag
I see. So instead 2 blocks, I use the one you put, that one will do the work correctly of both location blocks of my code
pmiranda avatar
ar flag
I did it in one location block, but I'm getting a loop of redirects
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.