惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
D
Docker
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
D
DataBreaches.Net
月光博客
月光博客
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
MyScale Blog
MyScale Blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学

NGINX Community Blog

NGINX Routing on HTTP Request Body HTTP Strict Transport Security Policy in NGINX Ingress Controller Predicate Routing for Native Handling of API Traffic – NGINX Community Blog Gateway API 1.6 conformance, external authentication, and fewer snippets – NGINX Community Blog HSTS without snippets, a faster start on large clusters, and improved configuration safety – NGINX Community Blog Control API, predicate locations, early body inspection, and more – NGINX Community Blog PROXY Protocol v2, Stricter Validation and More – NGINX Community Blog External Authentication Policy in NGINX Ingress Controller: A Real World Use Case – NGINX Community Blog External Authentication Policy in NGINX Ingress Controller: Patterns for VirtualServer and Ingress – NGINX Community Blog mTLS Policies in NGINX Ingress Controller – NGINX Community Blog Optimising NGINX Ingress Controller Startup Performance – NGINX Community Blog Meet With Us: NGINX Gateway Fabric & NGINX Ingress Controller Community Calls  – NGINX Community Blog Security, Performance, and Easier Migration – NGINX Community Blog Strengthening the NGINX Community – NGINX Community Blog F5 WAF for NGINX Comes to the Gateway API – NGINX Community Blog How NGINX Ingress Controller and NGINX Gateway Fabric Handle Kubernetes Backend Changes Natively – NGINX Community Blog Access Control Policy in NGINX Ingress Controller: Patterns for Ingress – NGINX Community Blog Enterprise-Grade Features and Gateway API 1.5 Conformance – NGINX Community Blog NGINX OSS 1.29.6 and 1.29.7: Open-sourced Session Persistence, Multipath TCP and More – NGINX Community Blog Keep-alive to upstreams is now default in NGINX 1.29.7 – NGINX Community Blog CORS Policy in NGINX Ingress Controller v5.4.0: Patterns for VirtualServer and Ingress – NGINX Community Blog A Community Hub for NGINX on Kubernetes, Including a New Ingress-NGINX Migration Tool – NGINX Community Blog
Cache Policy in NGINX Ingress Controller: A Practical Gui...
Venktesh Shivam Patel · 2026-04-16 · via NGINX Community Blog

Caching is one of the fastest ways to reduce backend load and improve response latency in Kubernetes.

With NGINX Ingress Controller (NIC), you can define caching behavior as a first-class Policy resource and attach it to a VirtualServer or VirtualServerRoute. That keeps caching configuration explicit, reusable, and versioned with the rest of your traffic policy.

Across this guide, we’re focused on:

  • How the cache policy works in NGINX Ingress Controller.
  • Where to attach it in VirtualServer.
  • Why StatefulSet is important for persistent cache use-cases.

Why Use a Cache Policy Resource?

Putting cache settings in a Policy resource gives platform teams a cleaner separation of concerns:

  • Application routing stays in VirtualServer.
  • Caching behavior lives in a dedicated, reusable policy.
  • Updates can be rolled out without embedding raw snippets in every route.

This model also makes reviews easier because cache behavior is visible in one place.

How the Cache Policy Works in NGINX Ingress Controller

At a high level:

  1. Create a Policy with a spec.cache block.
  2. Reference that policy from VirtualServer.spec.policies (server-wide) or from route-level policies.
  3. NGINX Ingress Controller renders the corresponding NGINX cache directives and applies them during config reload.

Example Cache Policy:

apiVersion: k8s.nginx.org/v1
kind: Policy
metadata:
  name: cache-policy
spec:
  cache:
    cacheZoneName: "testcache" # Required
    cacheZoneSize: "15m" # Required
    allowedCodes: ["any"] # Optional ["any"] or [200, 301, ...], "any" cannot be combined with specific codes
    allowedMethods: ["GET", "HEAD", "POST"] # Optional
    overrideUpstreamCache: true # Optional, default is false - whether to respect upstream cache-control headers (Cache-Control Expires Set-Cookie Vary X-Accel-Expires)

Example of VirtualServer referencing above Cache Policy:

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: cafe
spec:
  policies:
  - name: cache-policy
  host: cafe.example.com
  tls:
    secret: tls-secret
  upstreams:
  - name: tea
    service: tea-svc
    port: 80
  - name: coffee
    service: coffee-svc
    port: 80
  routes:
  - path: /tea
    action:
      pass: tea
  - path: /coffee
    action:
      pass: coffee

Important behavior to remember:

  • Cache policy is designed for VirtualServer and VirtualServerRoute flows.
  • If multiple cache policies are referenced for the same effective location, only one is applied (first one wins).
  • Route-level policies override same-type policies defined at VirtualServer.spec.

Key Fields Worth Tuning First

When you define spec.cache, prioritize these fields first:

  • cacheZoneName and cacheZoneSize: memory zone identity and capacity.
  • allowedMethods: which methods are cacheable.
  • allowedCodes plus time: what status codes are cached and for how long.
  • cacheKey: request identity for cache lookup.
  • overrideUpstreamCache: whether upstream cache headers should be honored.

Then tune advanced behavior only as needed:

  • cacheUseStale, cacheBackgroundUpdate, and cacheRevalidate for resilience.
  • conditions.noCache and conditions.bypass for selective caching.
  • cachePurgeAllow (NGINX Plus) for controlled invalidation.

Caching at Scale: Use StatefulSet for Persistent Cache Workloads

NGINX Ingress Controller supports running the controller as a StatefulSet, which is the better fit for disk-backed cache use-cases. Each replica gets stable storage through a PersistentVolume, which improves cache warm-up behavior after restarts.

For Helm-based deployments, NGINX Ingress Controller explicitly supports this model with:

  • controller.kind: statefulset
  • StatefulSet-specific nginxCachePVC configuration under controller.statefulset

You can find a complete working example on github and more documentation in our docs:

NGINX Community Forum