> ## Content Index
> Fetch the complete content index at: https://www.andreacasarin.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Nginx caching on Kubernetes
- URL: https://www.andreacasarin.com/2022/09/11/nginx-caching-on-kubernetes/
- Published: 2022-09-11T08:43:00.000Z
- Updated: 2026-10-30T11:28:13.000Z
- Author: Andrea Casarin
- Tags: #Import 2023-03-16 18:53

Nginx ingress is one of the best ingress you can use on your K8S cluster, by default it comes with a lot of features but caching is not enabled. Let's see how to set up a simple static cache for our websites.

On cluster ingress (this is managed by an Nginx config map):

```
proxy_cache_path /tmp/nginx-cache levels=1:2 keys_zone=static-cache:32m max_size=5g inactive=7d use_temp_path=off;
proxy_cache_valid any 5m;
proxy_cache_valid 200 30m;
```

On application ingress (managed via deploy):

```
proxy_ignore_headers Set-Cookie;
proxy_cache static-cache;

```

Watch out for proxy\_ignore\_headers, these could cache cookies and/or break sessions. You could use something like:

```
  if ($uri ~ "/admin/") {
    set $app_nocache 1;
  }
  if ($http_cookie ~* "logged_in") {
    set $arg_nocache 1;
  }
  proxy_no_cache $cookie_nocache $arg_nocache $app_nocache;

```

To avoid caching dynamic pages/admin areas.

And to add a nice touch:

```
add_header X-Cache-Status $upstream_cache_status;

```

So that you can watch your response headers to check the cache status for that particular request.

Enjoy!