0
0
Nginxdevops~10 mins

Proxy cache key in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Proxy cache key
Client Request Received
Extract Cache Key Components
Build Cache Key String
Check Cache for Key
Serve Cached
Store Response
Serve Response
The proxy cache key is built from parts of the client request to identify cached content. Nginx uses this key to find or store cached responses.
Execution Sample
Nginx
proxy_cache_key "$scheme://$host$request_uri";
This sets the cache key to the full URL including scheme, host, and request URI.
Process Table
StepActionValue of $schemeValue of $hostValue of $request_uriConstructed cache keyCache lookup resultNext action
1Receive requesthttpsexample.com/index.htmlStart processing
2Extract variableshttpsexample.com/index.htmlVariables ready
3Build cache keyhttpsexample.com/index.htmlhttps://example.com/index.htmlCache key formed
4Check cachehttps://example.com/index.htmlMissFetch from backend
5Fetch responseGet fresh content
6Store response in cachehttps://example.com/index.htmlCache updated
7Serve responseResponse sent to client
8Next request same keyhttpsexample.com/index.htmlhttps://example.com/index.htmlHitServe cached content
💡 Cache key lookup ends after serving cached or fresh response.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 8
$schemehttpshttpshttpshttps
$hostexample.comexample.comexample.comexample.com
$request_uri/index.html/index.html/index.html/index.html
cache_keyhttps://example.com/index.htmlhttps://example.com/index.htmlhttps://example.com/index.html
cache_statusMissHit
Key Moments - 3 Insights
Why does the cache key include $scheme, $host, and $request_uri?
Including these variables ensures the cache key uniquely identifies the full URL requested, avoiding serving wrong cached content. See execution_table step 3 where the key is built.
What happens if the cache key is not found in cache?
Nginx fetches the content from the backend server, stores it in cache with the key, then serves it. This is shown in execution_table steps 4 to 7.
Can the cache key be customized?
Yes, you can include or exclude variables to control cache granularity. The example uses a simple full URL key, but you can add headers or cookies if needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the constructed cache key?
Ahttps://example.com/
Bhttp://example.com/index.html
Chttps://example.com/index.html
D/index.html
💡 Hint
Check the 'Constructed cache key' column at step 3 in the execution_table.
At which step does Nginx find a cache miss?
AStep 2
BStep 4
CStep 6
DStep 8
💡 Hint
Look at the 'Cache lookup result' column in the execution_table.
If the $host changes, what happens to the cache key?
ACache key changes
BCache key stays the same
CCache key becomes empty
DCache key is ignored
💡 Hint
Refer to variable_tracker for $host and cache_key values.
Concept Snapshot
proxy_cache_key directive builds a unique key from request parts.
Common variables: $scheme, $host, $request_uri.
Nginx uses this key to find or store cached responses.
Changing variables changes cache key and cache entries.
Proper key ensures correct cached content served.
Full Transcript
When Nginx receives a client request, it extracts parts like scheme, host, and request URI. These parts are combined to form a cache key string. Nginx checks if this key exists in the cache. If found (cache hit), it serves the cached content. If not found (cache miss), it fetches fresh content from the backend, stores it in cache with that key, and serves it. The cache key must uniquely identify the requested resource to avoid serving wrong content. You can customize the cache key by including different variables. This process ensures efficient and correct caching behavior.