Nginx,其他内部条件位置块

kb5ga3dv  于 2022-11-21  发布在  Nginx
关注(0)|答案(1)|浏览(186)

目前我有这2个位置块(其中):

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

最后一个不起作用,我想把它移到第一个里面,条件是:

location ~ ^/myapp {
  try_files $uri /myapp/index.html;
  if (location /myapp/readiness) {
    access_log off;
    log_not_found off;
  }
}

这是可能的吗?或者我怎么能仅仅为了/myapp/ready而关闭acces_log呢?
编辑:这是完整的nginx.config文件,没有我需要放置的位置块:

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

7kjnsjlb1#

第二个位置块未被选择,因为URI /myapp/readiness与正则表达式^/myapp匹配,并且管理nginx如何选择位置的规则规定 * 正则表达式 * 位置优先于 * 前缀 * 位置。
如果只使用前缀位置,则逻辑可能会按预期方式工作:

location /myapp { ... }
location /myapp/readiness { ... }

请参阅location指示词文件。

相关问题