Nginx,排除访问日志的特定 URL

Nginx,排除访问日志的特定 URL

我需要从日志中排除特定的 URL。

我的日志中出现了很多这样的内容:

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"

我在许多区块的末尾尝试过这个location

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

摘自:

如何在此 nginx 配置中关闭仅针对某个 URL 的访问日志?

但它不起作用,这就是我再次询问的原因。

我还可以做些什么?

编辑:

完整的 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;
}

编辑2:

我尝试通过改变:

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

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

和:

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

但我得到了这个循环:

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" "-"

答案1

原因是以下块的组合:

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

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

作为nginx 文档解释一下,nginx 使用下面的方法来评估location块:

为了找到与给定请求匹配的位置,nginx 首先检查使用前缀字符串(前缀位置)定义的位置。从中,选择并记住具有最长匹配前缀的位置。然后按照正则表达式在配置文件中出现的顺序检查正则表达式。正则表达式的搜索在第一次匹配时终止,并使用相应的配置。

就您而言,当请求路径为时,会发生以下情况/myapp/readiness

  1. nginx 看到一个匹配的/myapp/readiness location阻止,并记住它。
  2. nginx 查看正则表达式,发现请求与 匹配~ ^/myapp location。因此它使用该块来处理请求。

为了防止 nginx 查看正则表达式location块,可以使用以下定义:

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

您需要try_files从此处的另一个块复制语句,因为否则 nginx 将使用默认值try_files

顺便说一句,您的配置有许多基于正则表达式的location块,尽管正常的前缀匹配就足够了。

相关内容