Bird
Raised Fist0
Nginxdevops~20 mins

Cache validity rules in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cache Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Cache validity with expires directive
What is the HTTP response header Expires value when the following nginx configuration is applied?

location /images/ {
  expires 10d;
}
AExpires header is set to 10 minutes from the response time
BExpires header is set to 10 seconds from the response time
CExpires header is not set by this configuration
DExpires header is set to 10 days from the response time
Attempts:
2 left
💡 Hint
The expires directive sets the cache duration relative to the response time.
🧠 Conceptual
intermediate
1:30remaining
Understanding cache-control max-age
Which statement correctly describes the effect of add_header Cache-Control "max-age=3600"; in nginx configuration?
AIt sets the cache to expire 3600 seconds after the response is received
BIt disables caching for the resource
CIt sets the cache to expire immediately
DIt sets the cache to expire after 3600 minutes
Attempts:
2 left
💡 Hint
max-age is measured in seconds.
❓ Troubleshoot
advanced
2:00remaining
Diagnosing cache not expiring as expected
You configured nginx with expires 5m; but clients still receive cached content after 10 minutes. What is the most likely cause?
AThe upstream server is sending conflicting Cache-Control headers that override nginx
BThe <code>expires</code> directive only works for static files, not dynamic content
CThe <code>expires</code> directive requires a reload of nginx to take effect
DThe client browser ignores all cache headers by default
Attempts:
2 left
💡 Hint
Check headers sent by the backend server.
🔀 Workflow
advanced
2:00remaining
Configuring cache validity for API responses
You want to cache API responses for 30 seconds but ensure clients always revalidate after that. Which nginx configuration snippet achieves this?
Aexpires 30s; add_header Cache-Control "no-cache";
Bexpires 30s; add_header Cache-Control "public";
Cadd_header Cache-Control "max-age=30, must-revalidate";
Dadd_header Cache-Control "no-store";
Attempts:
2 left
💡 Hint
Use Cache-Control directives to control revalidation.
✅ Best Practice
expert
2:30remaining
Best practice for cache validity with varying content
You serve images that rarely change and JSON data that updates frequently. What is the best cache validity strategy in nginx to optimize performance and freshness?
ASet the same expires time (e.g., 1d) for both images and JSON for simplicity
BSet long expires (e.g., 30d) for images and short max-age (e.g., 10s) with must-revalidate for JSON
CDisable caching for images and set long max-age for JSON
DUse no-store for both images and JSON to avoid caching issues
Attempts:
2 left
💡 Hint
Different content types need different cache durations.

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