9

I'm trying to configure cache for a specific host, but getting 404. Also It seems my config was not included into final nginx.conf. This file doesn't contain it

My 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

If I remove this sample

  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;

everything works but without cache

even if I have one line from snippet above It produces 404 error and doesn't work

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

1 Answers1

9

To enable caching, you need to configure the proxy_cache_path for the nginx-ingress-controller.
You can do it by modifying the ConfigMap for nginx-ingress-controller.


I've created an example to illustrate you how it works (I assume you have kubernetes/ingress-nginx).

First, create a ConfigMap named ingress-nginx-controller as described in the documentation custom_configuration:
Note: You may need to modify the proxy_cache_path settings, but shared memory zone (keys_zone=static-cache) should be the same as in your proxy_cache directive.

$ 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

And then create the Ingress resource ( I've modified your ingress resource a bit to demonstrate how X-Cache-Status header works):

$ 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

Finally we can check:

$ 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

More information on proxy_cache_path and proxy_cache can be found here.

matt_j
  • 4,010
  • 1
  • 9
  • 23