0
0
Nginxdevops~10 mins

Cache validity rules in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Cache validity rules
Request Received
Check Cache
Is Cached Content Present?
NoFetch from Origin
Yes
Check Cache Validity Rules
Expires?
Is Cache Valid?
NoFetch from Origin
Yes
Serve Cached Content
End
The flow shows how nginx checks if cached content is present and valid using cache validity rules before serving or fetching fresh content.
Execution Sample
Nginx
proxy_cache_path /data/nginx/cache keys_zone=mycache:10m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
This config sets cache storage, cache key, and cache validity times for HTTP status codes 200, 302, and 404.
Process Table
StepCache StatusHTTP StatusCache Validity RuleCache Valid?Action
1No cache entry--N/AFetch from origin
2Cache entry present200proxy_cache_valid 200 302 10mYes (within 10m)Serve cached content
3Cache entry present200proxy_cache_valid 200 302 10mNo (expired)Fetch from origin
4Cache entry present404proxy_cache_valid 404 1mYes (within 1m)Serve cached content
5Cache entry present404proxy_cache_valid 404 1mNo (expired)Fetch from origin
6Cache entry present500No cache validity ruleNoFetch from origin
💡 Execution stops when content is served from cache or fetched fresh due to cache miss or expiry.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6
Cache Entry PresentNoYesYesYesYesYes
HTTP Status-200200404404500
Cache Validity-Valid (within 10m)ExpiredValid (within 1m)ExpiredNo rule
Action Taken-Serve cacheFetch originServe cacheFetch originFetch origin
Key Moments - 3 Insights
Why does nginx fetch from origin when HTTP status is 500 even if cache entry exists?
Because there is no cache validity rule defined for status 500, nginx treats the cache as invalid and fetches fresh content (see execution_table step 6).
What happens if cached content for status 200 is older than 10 minutes?
Nginx considers the cache expired and fetches fresh content from origin (see execution_table step 3).
How does nginx decide cache validity for different HTTP statuses?
It uses proxy_cache_valid directives specifying time durations per status code (see execution_sample and execution_table steps 2,4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what action does nginx take at step 4 when HTTP status is 404 and cache is valid?
AServe cached content
BFetch from origin
CIgnore cache and serve error
DDelete cache entry
💡 Hint
Check the 'Action' column in execution_table row for step 4.
At which step does the cache expire for HTTP status 200?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look at 'Cache Valid?' column for HTTP status 200 in execution_table.
If you add 'proxy_cache_valid 500 5m;' how would step 6 change?
ACache would be deleted immediately
BCache would be considered valid for 5 minutes
CCache would still be invalid
DNo change in behavior
💡 Hint
Refer to how proxy_cache_valid rules affect cache validity in execution_table.
Concept Snapshot
nginx cache validity rules:
- Use proxy_cache_valid to set cache time per HTTP status
- Cached content served only if valid
- Expired or missing cache triggers origin fetch
- No rule means no caching for that status
- Helps control freshness of cached responses
Full Transcript
This visual execution shows how nginx handles cache validity rules. When a request arrives, nginx checks if cached content exists. If no cache is present, it fetches fresh content from the origin server. If cache exists, nginx checks validity based on HTTP status and proxy_cache_valid rules. For example, status 200 and 302 are cached for 10 minutes, 404 for 1 minute. If cached content is valid, nginx serves it directly. If expired or no rule applies, nginx fetches fresh content. This process ensures users get fresh content while benefiting from caching when possible.