
























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:
VirtualServer.Putting cache settings in a Policy resource gives platform teams a cleaner separation of concerns:
VirtualServer.This model also makes reviews easier because cache behavior is visible in one place.
At a high level:
Policy with a spec.cache block.VirtualServer.spec.policies (server-wide) or from route-level policies.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:
VirtualServer and VirtualServerRoute flows.VirtualServer.spec.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.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: statefulsetnginxCachePVC configuration under controller.statefulsetYou can find a complete working example on github and more documentation in our docs:

此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。