Nginx入口控制器缓存未命中

gcuhipw9  于 2023-02-11  发布在  Nginx
关注(0)|答案(1)|浏览(179)

我们在Kubernetes(EKS)环境中使用了这里描述的Nginx入口控制器映像(https://docs.nginx.com/nginx-ingress-controller/),在尝试实现缓存时遇到了很大的问题。
在入口控制器后面有一个基于JSON的服务。
Ingress生成如下所示的Nginx配置:

# configuration for dcjson-mlang25/terminology-ingress

upstream dcjson-mlang25-terminology-ingress-mlang25.test.domain-jsonserver-authoring-8080 {
    zone dcjson-mlang25-terminology-ingress-mlang25.test.domain-jsonserver-authoring-8080 256k;
    random two least_conn;
    server 10.220.2.66:8080 max_fails=1 fail_timeout=10s max_conns=0;
}

server {
    listen 80;
    listen [::]:80;

    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate /etc/nginx/secrets/dcjson-mlang25-jsonserver-tls-secret;
    ssl_certificate_key /etc/nginx/secrets/dcjson-mlang25-jsonserver-tls-secret;

    server_tokens on;
    server_name mlang25.test.domain;

    set $resource_type "ingress";
    set $resource_name "terminology-ingress";
    set $resource_namespace "dcjson-mlang25";

    if ($scheme = http) {
            return 301 https://$host:443$request_uri;
    }

    location /authoring/ {
            set $service "jsonserver-authoring";
            proxy_http_version 1.1;
            proxy_cache STATIC;
            proxy_cache_valid 200 1d;
            proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
            proxy_cache_revalidate on;
            proxy_set_header Connection "";
            proxy_hide_header 'Access-Control-Allow-Origin';
            proxy_hide_header 'Access-Control-Allow-Methods';
            proxy_hide_header 'Access-Control-Allow-Headers';
            proxy_hide_header 'Access-Control-Expose-Headers';
            proxy_hide_header 'Access-Control-Allow-Credentials';
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'PUT, GET, POST, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,AcceptX-FHIR-Starter,Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,Prefer,Pragma,If-Match,If-None-Match' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header X-Cache-Status $upstream_cache_status;
            proxy_connect_timeout 60s;
            proxy_read_timeout 1800s;
            proxy_send_timeout 1800s;
            client_max_body_size 4096m;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_buffering on;
            proxy_buffers 4 256k;
            proxy_buffer_size 128k;
            proxy_max_temp_file_size 4096m;

            proxy_pass http://dcjson-mlang25-terminology-ingress-mlang25.test.domain-jsonserver-authoring-8080/;
    }
}

Nginx.conf文件本身声明该高速缓存如下:

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=STATIC:32m inactive=24h max_size=10g;
proxy_cache_key $scheme$proxy_host$request_uri;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

map $upstream_trailer_grpc_status $grpc_status {
    default $upstream_trailer_grpc_status;
    '' $sent_http_grpc_status;
}
** snipped**
}

后端应用程序不返回任何设置Cookie头,我知道这是一个问题-它不是。
当放置一个简单的GET请求时,我在Nginx日志中看到这个

2023/02/07 20:46:49 [debug] 416#416: *171 http script var: "https"
2023/02/07 20:46:49 [debug] 416#416: *171 http script var: "dcjson-mlang25-terminology-ingress-mlang25.test.domain-jsonserver-authoring-8080"
2023/02/07 20:46:49 [debug] 416#416: *171 http script var: "/authoring/fhir/CodeSystem/genenames.geneId-small"
2023/02/07 20:46:49 [debug] 416#416: *171 http cache key: "httpsdcjson-mlang25-terminology-ingress-mlang25.test.domain-jsonserver-authoring-8080/authoring/fhir/CodeSystem/genenames.geneId-small"
2023/02/07 20:46:49 [debug] 416#416: *171 add cleanup: 000055C5DDA4ED00
2023/02/07 20:46:49 [debug] 416#416: shmtx lock
2023/02/07 20:46:49 [debug] 416#416: slab alloc: 120 slot: 4
2023/02/07 20:46:49 [debug] 416#416: slab alloc: 00007FECD6324080
2023/02/07 20:46:49 [debug] 416#416: shmtx unlock
2023/02/07 20:46:49 [debug] 416#416: *171 http file cache exists: -5 e:0
2023/02/07 20:46:49 [debug] 416#416: *171 cache file: "/tmp/nginx_cache/8/b4/9ac307cbf4540372616c09cd894b9b48"

几秒钟后重复的请求看起来完全一样。在我看来,这是在说该高速缓存没有命中吗?
每个响应标头集看起来都像这样,状态总是MISS

2023/02/07 20:46:49 [debug] 416#416: *171 HTTP/1.1 200
Server: nginx/1.23.2
Date: Tue, 07 Feb 2023 20:46:49 GMT
Content-Type: application/fhir+json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Request-Id: sJ4yXmP1ziSF3fJt
Cache-Control: no-cache
Vary: Accept,Origin,Accept-Encoding,Accept-Language,Authorization
X-Powered-By: HAPI FHIR 6.0.0 REST Server (FHIR Server; FHIR 4.0.1/R4)
ETag: W/"1"
Content-Location:             https://mlang25.test.domain/authoring/fhir/CodeSystem/genenames.geneId-small/_history/1
Last-Modified: Tue, 07 Feb 2023 20:08:35 GMT
Content-Encoding: gzip
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-    With,If-Modified-Since,Cache-Control,Content-Type,Authorization,AcceptX-FHIR-    Starter,Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-    Control-Request-Headers,Authorization,Prefer,Pragma,If-Match,If-None-Match
Access-Control-Expose-Headers: Content-Length,Content-Range
Access-Control-Allow-Credentials: true
X-Cache-Status: MISS

我真的很难弄清楚为什么该高速缓存从来没有被命中。

dxpyg8gm

dxpyg8gm1#

对于任何人谁偶然发现这一点-我们的后端有一个变化,从第三方,并已开始返回缓存控制无缓存意味着nginx将永远不会缓存的结果。

相关问题