<p>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</p>
<p>My ingress.yaml:</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: images-ingress
  labels:
    last_updated: &quot;14940999355&quot;
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    cert-manager.io/cluster-issuer: &quot;letsencrypt-prod&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: 8m
    nginx.ingress.kubernetes.io/proxy-buffering: &quot;on&quot;
    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
</code></pre>
<p>If I remove this sample</p>
<pre><code>  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;
</code></pre>
<p>everything works but without cache</p>
<p>even if I have one line from snippet above It produces 404 error and doesn't work</p>
<pre><code>  nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;
</code></pre>

Сергей Коновалов
<p>To enable caching, you need to configure the <a href="https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/#enabling-the-caching-of-responses" rel="noreferrer">proxy_cache_path</a> for the <code>nginx-ingress-controller</code>.<br />
You can do it by modifying the <code>ConfigMap</code> for <code>nginx-ingress-controller</code>.</p>
<hr />
<p>I've created an example to illustrate you how it works (I assume you have <a href="https://kubernetes.github.io/ingress-nginx/" rel="noreferrer">kubernetes/ingress-nginx</a>).</p>
<p>First, create a <code>ConfigMap</code> named <code>ingress-nginx-controller</code> as described in the documentation <a href="https://kubernetes.github.io/ingress-nginx/examples/customization/custom-configuration/#custom-configuration" rel="noreferrer">custom_configuration</a>:<br />
<strong>Note:</strong> You may need to modify the <code>proxy_cache_path</code> settings, but shared memory zone (keys_zone=<strong>static-cache</strong>) should be the same as in your <code>proxy_cache</code> directive.</p>
<pre><code>$ cat configmap.yml
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: ingress-nginx-controller
  namespace: default
data:
  http-snippet: &quot;proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=static-cache:10m max_size=10g  inactive=60m use_temp_path=off;&quot;
  
$ kubectl apply -f configmap.yml 
configmap/ingress-nginx-controller configured
</code></pre>
<p>And then create the <code>Ingress</code> resource ( I've modified your ingress resource a bit to demonstrate how <code>X-Cache-Status</code> header works):</p>
<pre><code>$ cat ingress.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: images-ingress
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: 8m
    nginx.ingress.kubernetes.io/proxy-buffering: &quot;on&quot;
    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
</code></pre>
<p>Finally we can check:</p>
<pre><code>$ 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
</code></pre>
<p>More information on <code>proxy_cache_path</code> and <code>proxy_cache</code> can be found <a href="https://www.nginx.com/blog/nginx-caching-guide/#How-to-Set-Up-and-Configure-Basic-Caching" rel="noreferrer">here</a>.</p>
