proxy_cache_path needs to be configured globally for the nginx-ingress-controller, which means creating or updating the nginx-configuration config map:
---
apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: ingress-nginx
name: nginx-configuration
namespace: ingress-nginx
data:
http-snippet: "proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=mycache:32m use_temp_path=off max_size=4g inactive=24h;"
And then in each ingress, a server-snippet annotation needs to be added to turn on caching for that ingress:
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
annotations:
kubernetes.io/ingress.class: nginx
ingress.kubernetes.io/ssl-redirect: "true"
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 mycache;
proxy_cache_lock on;
proxy_cache_valid any 60m;
proxy_ignore_headers Cache-Control;
add_header X-Cache-Status $upstream_cache_status;
spec:
...
See the linked references for other proxy-related options that can be set. It's also useful to look at the nginx.conf generated by the nginx-ingress-controller to see what it's already setting and where your config goes. Some configurations just aren't possible without using a custom template or your own nginx.conf.
WARNING: Be sure to test any sites using this configuration and check for issues like being unable to log in, or users able to access restricted areas without logging in.
References
- https://nginx.org/en/docs/http/ngx_http_proxy_module.html
- https://github.com/kubernetes/ingress-nginx/issues/739
- https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations
- https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap
- https://kubernetes.io/docs/concepts/services-networking/ingress/