Setting cache-control headers for nginx ingress controller on kubernetes GKE

前端 未结 2 1450
醉梦人生
醉梦人生 2020-12-17 05:57

I have an ingress-nginx controller handling traffic to my Kubernetes cluster hosted on GKE. I set it up using helm installation instructions from docs:

Docs here

相关标签:
2条回答
  • 2020-12-17 06:15

    configuration-snippet will work with nginx ingress

    you can use something like

    nginx.ingress.kubernetes.io/configuration-snippet : |
          if ($request_uri ~* \.(js|css|gif|jpe?g|png)) {
            expires 1M;
            add_header Cache-Control "public";
          }
    
    0 讨论(0)
  • 2020-12-17 06:21

    Those location blocks are last and/or longest match wins, and since the ingress itself is not serving any such content, the nginx relies on a proxy_pass directive pointing at the upstream server. Thus, if you are getting 404s, it's very likely because your location is matching, thus interfering with the proxy_pass one. There's a pretty good chance you'd actually want configuration-snippet: instead, likely in combination with if ($request_uri ~* ...) { to add the header.

    One can try this locally with a trivial nginx.conf pointing at python3 -m http.server 9090 or whatever fake upstream target.

    Separately, for debugging nginx ingress problems, it is often invaluable to consult its actual nginx.conf, which one can grab from any one of the ingress Pods, and/or consulting the logs of the ingress Pods where nginx will emit helpful debugging text.

    0 讨论(0)
提交回复
热议问题