Ingress nginx缓存

6

我正在尝试弄清楚如何使用nginx代理缓存并应用一些特定规则。例如,当我托管Ghost或WordPress时,我不希望缓存管理部分。使用服务器片段,我尝试了很多不同的组合,但在管理员部分仍然存在缓存问题。

nginx.ingress.kubernetes.io/proxy-buffering: "on"
nginx.ingress.kubernetes.io/server-snippet: |-
  proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
  proxy_ignore_headers Set-Cookie;
  proxy_cache app_cache;
  proxy_cache_lock on;
  proxy_cache_valid any 30m;
  add_header X-Cache-Status $upstream_cache_status;

我希望你能为(ghost | sinout)路径使用nginx代码片段,在管理区域时绕过缓存,但我丢失了proxy_pass上下文,导致502网关错误。 这是当前的Ingress配置,缓存每个页面,包括管理员路径:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/proxy-buffering: "on"
    nginx.ingress.kubernetes.io/server-snippet: |-
      proxy_cache my_blog_cache;
      proxy_cache_lock on;
      proxy_cache_valid any 30m;
      add_header X-Cache-Status $upstream_cache_status;
      proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
  name: my-blog
  namespace: web
spec:
  rules:
  - host: blog.example.com
    http:
      paths:
      - backend:
          serviceName: ingress-541322b8660dbd2ceb1e8ff1813f0dd5
          servicePort: 2368
        path: /
  tls:
  - hosts:
    - blog.example.com
    secretName: my-blog-cert
status:
  loadBalancer:
    ingress:
    - ip: 1.2.3.4

这是我尝试获取的nginx配置,但不兼容Ingress注释:

   location / {
        proxy_cache my_blog_cache;
        proxy_cache_valid 200 30m;
        proxy_cache_valid 404 1m;
        proxy_pass http://ghost_upstream;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
        proxy_ignore_headers Set-Cookie;
        proxy_hide_header Set-Cookie;
        proxy_hide_header X-powered-by;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        expires 10m;
    }
    location /content/images {
        alias /path/to/ghost/content/images;
        access_log off;
        expires max;
    }
    location /assets {
        alias /path/to/ghost/content/themes/uno-master/assets;
        access_log off;
        expires max;
    }
    location /public {
        alias /path/to/ghost/core/built/public;
        access_log off;
        expires max;
    }
    location /ghost/scripts {
        alias /path/to/ghost/core/built/scripts;
        access_log off;
        expires max;
    }
    location ~ ^/(?:ghost|signout) { 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://ghost_upstream;
        add_header Cache-Control "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0";
    }

感谢您的帮助!
2个回答

6

我有同样的需求:在Kubernetes集群中的Nginx Ingress上直接缓存Ghost响应与它们的Cache-Control头。

经过几个小时的努力,这是我的解决方案:

第一步

首先,您需要在Nginx Ingress ConfigMap level上定义proxy_cache_path(文档对如何应用它真的不清楚)。

在我的情况下,我通过Helm管理Nginx Ingress安装,所以我将其添加到了Helm值图表中:

# Default values https://github.com/kubernetes/ingress-nginx/blob/main/charts/ingress-nginx/values.yaml
controller:
  config:
    http-snippet: "proxy_cache_path /tmp/nginx_my_cache levels=1:2 keys_zone=mycache:2m use_temp_path=off max_size=2g inactive=48h;"

然后应用这个变化:
helm upgrade -f my-nginx-ingress-values.yaml ingress-nginx ingress-nginx/ingress-nginx --recreate-pods

第二步

既然我们已经设置了proxy_cache_path,我们需要使用注释为特定主机配置Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myingress
  namespace: mynamespace
  annotations:
    kubernetes.io/ingress.class: "nginx"
    # Buffering must be enabled for Nginx disk cache to work.
    nginx.ingress.kubernetes.io/proxy-buffering: "on"
    # See https://www.nginx.com/blog/nginx-caching-guide/
    # Cache Key Zone is configured in Helm config.
    nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache mycache;
      proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
      proxy_cache_background_update on;
      proxy_cache_revalidate on;
      proxy_cache_lock on;
      add_header X-Cache-Status $upstream_cache_status;

注意:
我花费了大部分时间弄清楚为什么我仍然得到“MISS”的原因。 原来是由于nginx.ingress.kubernetes.io/proxy-buffering 默认在Ingress中 - off - 这会禁用Nginx缓存,因此您必须将其设置为on,这就是我们所做的。
将更改应用于Ingress。
调试生成的Nginx配置
您可以(并且我认为应该)验证作为应用ConfigMap和Ingress级别注释的结果生成的Ingress所使用的nginx.conf的内容。
为此,您可以将nginx.conf从Ingress Controller pod复制到本地计算机并验证其内容(或exec进入pod并在那里查看它)。
# Make sure to use correct namespace where Ingress Controller is deployed
# and correct Ingress Controller Pod name
kubectl cp -n default ingress-nginx-controller-xxxx:/etc/nginx/nginx.conf ~/Desktop/nginx.conf

它应该包含我们所做的所有更改!

调试实际响应缓存

现在我们已经完成了所有配置 - 是时候验证实际缓存了。请注意,我们添加了X-Cache-Status标头,它将指示是否为HITMISS

我个人喜欢使用httpie从终端进行HTTP请求,您也可以使用curl或浏览器:

第一个请求将是MISS

http https://example.com/myimage.jpg
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: public, max-age=31536000
Connection: keep-alive
Content-Length: 53588
Content-Type: image/jpeg
Date: Wed, 20 Oct 2021 10:39:06 GMT
ETag: W/"d154-17c3aa43389"
Last-Modified: Fri, 01 Oct 2021 06:56:52 GMT
Strict-Transport-Security: max-age=15724800; includeSubDomains
X-Cache-Status: HIT
X-Powered-By: Express
X-Request-ID: 0c73f97cb51d3071f14968720a26a99a

+-----------------------------------------+
| NOTE: binary data not shown in terminal |
+-----------------------------------------+

现在对同一URL的第二个请求是一个HIT,不会命中实际的Ghost安装,成功!

http https://example.com/myimage.jpg
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: public, max-age=31536000
Connection: keep-alive
Content-Length: 53588
Content-Type: image/jpeg
Date: Wed, 20 Oct 2021 10:39:43 GMT
ETag: W/"d154-17c3aa43389"
Last-Modified: Fri, 01 Oct 2021 06:56:52 GMT
Strict-Transport-Security: max-age=15724800; includeSubDomains
X-Cache-Status: HIT
X-Powered-By: Express
X-Request-ID: 0c73f97cb51d3071f14968720a26a99a

+-----------------------------------------+
| NOTE: binary data not shown in terminal |
+-----------------------------------------+

检查Ghost日志是很有用的,以确保缓存命中请求直接从Nginx提供而没有经过Ghost。



2
在nginx-ingress控制器中,有一些选项只能使用ConfigMap进行更改,而其他选项则可以使用注释进行更改,就像你所做的那样。
您可以将两者结合起来以达到预期结果,或创建一个自定义模板这里您可以看到一个可能会帮助您的替代方案。

谢谢你的回答。使用注释对于特定规则来说是相当有限的,而配置映射会影响到所有的入口吗?或者也许有一些方法可以将模板或配置映射附加到一个入口上?我已经找到了一种替代方法,即创建多个具有每个路径位置特定注释的入口。例如: 路径:/ -> 应用缓存,缓存状态=命中 路径:/ghost -> 没有注释,缓存状态=未命中 它起作用了!这是一个好的做法吗? - Liogate
如果所有的应用程序都获得相同的IP地址,那么创建多个入口没有问题,只需要注意未来的维护即可。正如您所说,注释对于自定义所有nginx来说是相当有限的,这就是为什么您可以选择与nginx模板选项结合使用的原因。 - Mr.KoopaKiller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接