Proxy cache storage and key
What a proxy cache stores
Imagine a catalog service that builds the same public document for many readers. Without a proxy cache, every request reaches that service. With a cache, Nginx can store an eligible upstream response and serve a later request from that copy. The upstream is the application behind Nginx; the origin counter in this chapter identifies how many times the application generated a response. This is separate from the browser cache from the previous chapter.
proxy_cache_path cache levels=1:2 keys_zone=public_docs:1m max_size=16m inactive=5m use_temp_path=off;Use this only with a private lab prefix. The zone public_docs reserves shared memory for lookup metadata; the response bytes live in files under cache. levels groups the files into subdirectories. The size target of 16 MB is managed over time, not a strict disk quota. inactive removes entries that have gone unused for five minutes, regardless of their remaining freshness. Keep an independent disk budget and monitor free space.
Enable caching only for a public route
location = /catalog { proxy_pass http://127.0.0.1:8091; proxy_cache public_docs; proxy_cache_key "$scheme|$host|$request_uri"; proxy_cache_valid 200 30s; proxy_cache_methods GET HEAD;}The location chooses the named zone. The validity rule provides a fallback lifetime for eligible 200 responses when upstream headers do not supply a policy. It does not force storage. GET and HEAD are the normal cacheable methods; leave writes out. HEAD is normally converted to GET for caching; if you disable that conversion, distinguish methods in the key. The complete lab adds explicit exclusion rules before using this route.
Choose the identity of a response
http|alpha.test|/catalog?edition=basichttp|beta.test|/catalog?edition=basichttp|alpha.test|/catalog?edition=plusThese three requests should not share a stored response. The lab accepts only two known hostnames on one HTTP listener and passes the selected host to one backend. The key contains scheme, host and original request URI, including the query. A deployment with multiple ports, upstream identities or other response-changing inputs must account for those dimensions too. Do not copy this key into a different architecture without reviewing the origin contract. Default Nginx keys use the upstream identity rather than necessarily distinguishing public virtual hosts.
The URI variable omits the query string and this key also omits host. A basic edition could be confused with a plus edition or a response from the other site. An indiscriminately large key also wastes space: keep meaningful inputs, do not insert raw credentials, and only normalize inputs when their equivalence is proven.
Practice
Write the keys for two hostnames and two editions. Explain why cache storage size and response freshness are different controls. Next: decide which requests must never read or write shared entries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Proxy cache storage and key
Build a small shared response cache and choose keys that keep public host and query variants separate.
Where are response bodies normally stored?
Which key separates host and query variants in this lab?
What does inactive=5m control?
Does max_size guarantee a hard filesystem quota?
Which methods are appropriate for the public read fixture?