Bird
Raised Fist0
Nginxdevops~10 mins

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

Choose your learning style10 modes available
Process Flow - Cache validity rules
Request Received
↓
Check Cache
↓
Is Cached Content Present?
No→Fetch from Origin
Yes↓
Check Cache Validity Rules
↓
Expires?
↓
Is Cache Valid?
No→Fetch 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.

Practice

(1/5)
1. What does the proxy_cache_valid directive in nginx control?
easy
A. The size limit of the cache storage
B. The format of cached files on disk
C. The IP addresses allowed to access the cache
D. How long cached content is considered fresh and valid

Solution

  1. Step 1: Understand the purpose of proxy_cache_valid

    This directive sets the time duration for which cached responses are considered fresh and can be served without revalidation.
  2. Step 2: Differentiate from other cache settings

    It does not control cache size, access permissions, or file formats, only the validity time of cached content.
  3. Final Answer:

    How long cached content is considered fresh and valid -> Option D
  4. Quick Check:

    Cache validity = duration freshness [OK]
Hint: Cache validity means freshness time of cached content [OK]
Common Mistakes:
  • Confusing cache size with cache validity
  • Thinking it controls access permissions
  • Assuming it changes file storage format
2. Which of the following is the correct syntax to set cache validity for HTTP 200 responses to 10 minutes in nginx?
easy
A. proxy_cache_valid 200 = 10m;
B. proxy_cache_valid 10m 200;
C. proxy_cache_valid 200 10m;
D. proxy_cache_valid 10m;

Solution

  1. Step 1: Recall the syntax format

    The correct syntax is proxy_cache_valid <code> <time>; where status code comes first, then time.
  2. Step 2: Check each option

    proxy_cache_valid 200 10m; matches the correct order: status code 200 followed by 10 minutes (10m). Others have wrong order or extra symbols.
  3. Final Answer:

    proxy_cache_valid 200 10m; -> Option C
  4. Quick Check:

    Status code then time = correct syntax [OK]
Hint: Status code first, then time duration in proxy_cache_valid [OK]
Common Mistakes:
  • Swapping time and status code order
  • Adding extra symbols like '='
  • Omitting status code when required
3. Given this nginx config snippet:
proxy_cache_valid 200 5m;
proxy_cache_valid 404 1m;

What happens when a cached 404 response is requested after 2 minutes?
medium
A. The cached 404 is served from cache
B. The cached 404 is considered expired and revalidated
C. The cache is cleared automatically
D. The server returns a 500 error

Solution

  1. Step 1: Understand cache validity times

    404 responses are cached for 1 minute. After 2 minutes, the cached 404 is expired.
  2. Step 2: Determine behavior after expiration

    Expired cache triggers revalidation or fetching fresh response, so cached 404 is not served directly.
  3. Final Answer:

    The cached 404 is considered expired and revalidated -> Option B
  4. Quick Check:

    Cache expired after 1m, so revalidation needed [OK]
Hint: Cache expires after set time, then revalidation occurs [OK]
Common Mistakes:
  • Assuming cache serves expired content
  • Thinking cache clears automatically after expiry
  • Confusing 404 cache with 200 cache time
4. You set proxy_cache_valid 200 10m; but cached responses are never served from cache. What is the likely error?
medium
A. The cache zone is not defined or enabled
B. The time format '10m' is invalid
C. The status code 200 is incorrect for caching
D. The directive must be inside http block only

Solution

  1. Step 1: Check cache zone setup

    Without defining and enabling a cache zone, caching won't work even if proxy_cache_valid is set.
  2. Step 2: Validate other options

    Time format '10m' is valid, status code 200 is correct, and directive can be inside server or location blocks too.
  3. Final Answer:

    The cache zone is not defined or enabled -> Option A
  4. Quick Check:

    Cache zone missing disables caching [OK]
Hint: Define and enable cache zone before setting cache validity [OK]
Common Mistakes:
  • Ignoring cache zone definition
  • Misreading time format as invalid
  • Assuming directive location is restricted
5. You want to cache HTTP 200 responses for 15 minutes, HTTP 404 for 2 minutes, and all other responses for 5 minutes. Which nginx config snippet correctly applies these cache validity rules?
hard
A. proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 5m;
B. proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid any 5m;
C. proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 1 5m;
D. proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 0 5m;

Solution

  1. Step 1: Set specific status codes first

    HTTP 200 and 404 have explicit cache times set correctly in all options.
  2. Step 2: Use no status code to match all other status codes

    In nginx, proxy_cache_valid time; without a status code applies to all responses not matched by prior specific directives.
  3. Step 3: Evaluate options

    proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 5m; (proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 5m;) is correct as the last line sets default for unmatched codes. proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 0 5m;'s '0' doesn't match valid statuses. 'any' (B) and '1' (C) are invalid for default.
  4. Final Answer:

    proxy_cache_valid 200 15m;\nproxy_cache_valid 404 2m;\nproxy_cache_valid 5m; -> Option A
  5. Quick Check:

    proxy_cache_valid time; = default for others [OK]
Hint: No status code = default cache validity for unmatched responses [OK]
Common Mistakes:
  • Using '0' for default (doesn't match statuses)
  • Using invalid keywords like 'any'
  • Using incorrect codes like '1' for default
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard