最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nginx - Kubernetes Ingress Controller. X-Cache-Status is always Bypass - Stack Overflow

programmeradmin1浏览0评论

Configured proxy_cache_path for keys_zone=static-cache via ConfigMap. I will check that this is exactly applied in the configuration. It does:

$ kubectl -n ingress-nginx exec -it pod/$POD_IC_NAME -- cat /etc/nginx/nginx.conf | grep proxy_cache_path
        proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=static-cache:10m max_size=10g  inactive=60m use_temp_path=off;
        proxy_cache_path /tmp/nginx/nginx-cache-auth levels=1:2 keys_zone=auth_cache:10m max_size=128m inactive=30m use_temp_path=off;

Now I'm configuring Ingress. I get a helm template like this:

{{ if .Values.createIngress }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ntp-integration-bff-2-{{ .Release.Name }}
  annotations:
    {{ if .Values.http2Enable }}
    nginx.ingress.kubernetes.io/enable-http2: "true"
    {{ end }}
    {{ if and .Values.httpCache .Values.services.ntp.integrationBff.httpCache }}
    nginx.ingress.kubernetes.io/server-snippet: |
      location ~* ^/api/v2/ {
        proxy_buffering on;
        proxy_cache static-cache;
        proxy_cache_valid 200 30m;
        proxy_cache_methods GET;
        proxy_cache_key "$scheme$request_uri";
        proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie" "Vary";
        proxy_hide_header Cache-Control;
        proxy_cache_bypass off;
        proxy_no_cache off;
        proxy_cache_lock on;
        proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
        proxy_cache_convert_head off;
        proxy_cache_min_uses 1;
        proxy_cache_background_update on;
        add_header X-Cache-Status $upstream_cache_status;
        # Директивы для проксирования заголовков
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://ntp-integration-bff-{{ .Release.Name }}.{{ .Release.Namespace }}.svc.cluster.local:8000;
      }
    {{ end }}
spec:
  ingressClassName: nginx
{{ if .Values.tls.enable }}
  tls:
  - hosts:
    - {{ .Values.ingress.api.publicDomain2 }}
    secretName: platform-tls-secret
{{ end }}
  rules:
  - host: {{ .Values.ingress.api.publicDomain2 }}
    http:
      paths:
      - path: /api/v3
        pathType: Prefix
        backend:
          service:
            name: ntp-integration-bff-{{ .Release.Name }}
            port:
              number: 8000
{{ end }}

I'm getting this configuration :

location ~* ^/api/v2/ {
        proxy_buffering on;
        proxy_cache static-cache;
        proxy_cache_valid 200 30m;
        proxy_cache_methods GET;
        proxy_cache_key "$scheme$request_uri";
        proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie" "Vary";
        proxy_hide_header Cache-Control;
        proxy_cache_bypass off;
        proxy_no_cache off;
        proxy_cache_lock on;
        proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
        proxy_cache_convert_head off;
        proxy_cache_min_uses 1;
        proxy_cache_background_update on;
        add_header X-Cache-Status $upstream_cache_status;
        # Директивы для проксирования заголовков
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass :8000;
}

But in the browser I see that all requests are returned with x-cache-status: BYPASS

REQUEST = /api/v2/query/ru/system-banner

Request Headers

GET /api/v2/query/ru/system-banner HTTP/1.1
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,id;q=0.6,zh-TW;q=0.5,zh;q=0.4,fr;q=0.3,bg;q=0.2
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJjOTlhMzc0Yi02YjliLTQzZTQtYjYxNy1hYmJkZDNhMjkyNjciLCJzdWIiOiJGQU1JTElZQV9JLk8iLCJleHAiOjE3Mzk1NDAxMjQsImlhdCI6MTczODY3NjEyNH07OS0yDQtQMjUYXnCDFlJtVoSSjuGpbhh_167geQ6YtuPHIotI507X-pFl2w3eIIzGZkfzmbEBjMe8oALVZ9Q
Connection: keep-alive
Cookie: username=...; backend=...; rt_a=eyJhbGciOiJIUzI1NiIsIn....; Authorization=Bearer%20eyJhbGciOiJIUz...; rt_r=qjkWLS8xkfcw...
Host: dev190.local
Referer: /gp-frontend-gp-main/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Response Headers

HTTP/1.1 200 OK
Date: Tue, 04 Feb 2025 13:53:51 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 447
Connection: keep-alive
X-Cache-Status: BYPASS

I don't understand what else can be configured to make these Nginx requests cached? According to my logic caching should already work.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>