【问题标题】:How to properly configure ingress cache to get it working?如何正确配置入口缓存以使其正常工作?
【发布时间】:2021-05-22 14:43:57
【问题描述】:

我正在尝试为特定主机配置缓存,但得到 404。此外,我的配置似乎未包含在最终的 nginx.conf 中。此文件不包含它

我的 ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: images-ingress
  labels:
    last_updated: "14940999355"
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: 8m
    nginx.ingress.kubernetes.io/proxy-buffering: "on"
    nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid 404 10m;
      proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
      proxy_cache_bypass $http_x_purge;
      add_header X-Cache-Status $upstream_cache_status;
spec:
  tls:
  - hosts:
    - static.qwstrs.com
    secretName: letsencrypt-prod
  rules:
  - host: static.qwstrs.com
    http:
      paths:
      - path: /
        backend:
          serviceName: imaginary
          servicePort: 9000

如果我删除这个样本

  nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid 404 10m;
      proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
      proxy_cache_bypass $http_x_purge;
      add_header X-Cache-Status $upstream_cache_status;

一切正常,但没有缓存

即使我上面的 sn-p 有一行它会产生 404 错误并且不起作用

  nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;

【问题讨论】:

    标签: kubernetes nginx-ingress microk8s


    【解决方案1】:

    要启用缓存,您需要为nginx-ingress-controller 配置proxy_cache_path
    您可以通过将ConfigMap 修改为nginx-ingress-controller 来实现。


    我创建了一个示例来说明它是如何工作的(我假设你有 kubernetes/ingress-nginx)。

    首先,创建一个名为ingress-nginx-controllerConfigMap,如文档custom_configuration 中所述:
    注意:您可能需要修改proxy_cache_path 设置,但共享内存区域(keys_zone=static-cache) 应该与您的 proxy_cache 指令中的相同。

    $ cat configmap.yml
    # configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: ingress-nginx-controller
      namespace: default
    data:
      http-snippet: "proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=static-cache:10m max_size=10g  inactive=60m use_temp_path=off;"
      
    $ kubectl apply -f configmap.yml 
    configmap/ingress-nginx-controller configured
    

    然后创建 Ingress 资源(我稍微修改了您的入口资源以演示 X-Cache-Status 标头的工作原理):

    $ cat ingress.yml
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: images-ingress
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/rewrite-target: /
        nginx.ingress.kubernetes.io/proxy-body-size: 8m
        nginx.ingress.kubernetes.io/proxy-buffering: "on"
        nginx.ingress.kubernetes.io/configuration-snippet: |
          proxy_cache static-cache;
          proxy_cache_valid any 60m;
          add_header X-Cache-Status $upstream_cache_status;
    spec:
      tls:
      - hosts:
        - static.qwstrs.com
        secretName: letsencrypt-prod
      rules:
      - host: static.qwstrs.com
        http:
          paths:
          - path: /
            backend:
              serviceName: imaginary
              servicePort: 9000
              
    $ kubectl apply -f ingress.yml
    ingress.extensions/images-ingress configured
    

    终于可以检查了:

    $ curl -k -I https://static.qwstrs.com
    HTTP/2 200
    ...
    x-cache-status: MISS
    accept-ranges: bytes
    
    $ curl -k -I https://static.qwstrs.com
    HTTP/2 200
    ...
    x-cache-status: HIT
    accept-ranges: bytes
    

    有关proxy_cache_pathproxy_cache 的更多信息,请访问here

    【讨论】:

    • 是的。谢谢
    猜你喜欢
    • 2021-11-17
    • 2012-07-28
    • 2016-07-30
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    相关资源
    最近更新 更多