我有完全相同的愿望:缓存 Ghost 响应Cache-Control
标头直接位于 Kubernetes 集群中的 Nginx Ingress 上。
经过几个小时的研究,这是我的解决方案:
第一步
首先,您需要定义proxy_cache_path
在 Nginx Ingress 上ConfigMap level(文档确实不清楚如何应用它)。
就我而言,我通过 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;
Note:
我花了大部分时间来弄清楚为什么我仍然得到MISS
es.原来是因为nginx.ingress.kubernetes.io/proxy-buffering
Ingress 中的默认值 — off
— 这会禁用 Nginx 缓存,因此您have将其设置为on
这就是我们所做的。
将更改应用到 Ingress。
调试 Nginx 配置结果
我认为你可以而且应该验证结果nginx.conf
用于应用后生成的 IngressConfigMap
和入口级注释。
为此,您可以复制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
标头将指示它是否是HIT
or MISS
.
我个人喜欢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。