我正在尝试为特定主机配置缓存,但得到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;
一切正常,但没有缓存
即使我从上面的代码片段中有一行,它也会产生404错误并且不能工作
nginx.ingress.kubernetes.io/server-snippet: |
proxy_cache static-cache;
要启用缓存,需要配置nginx-ingress-controller
的proxy_cache_path
可以通过修改nginx-ingress-controller
的ConfigMap
来实现。
我已经创建了一个例子来说明它是如何工作的(我假设你有kubernetes/ingress-nginx)。
首先,创建一个名为ingress-nginx-controller
的ConfigMap
,如文档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_path
和proxy_cache
的更多信息可以在这里找到。