Bird
Raised Fist0
Nginxdevops~20 mins

Proxy cache key in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Proxy Cache Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the effective cache key with this configuration?
Given this nginx proxy cache key configuration, what is the resulting cache key for a request with host 'example.com' and URI '/index.html'?
Nginx
proxy_cache_key "$host$request_uri";
A"example.com/index.html"
B"/index.html"
C"example.com"
D"$host$request_uri"
Attempts:
2 left
💡 Hint
The variables $host and $request_uri are replaced by their values in the cache key string.
🧠 Conceptual
intermediate
1:30remaining
Why customize proxy_cache_key in nginx?
What is the main reason to customize the proxy_cache_key in nginx proxy caching?
ATo disable caching completely
BTo control how requests are grouped and cached based on request properties
CTo set the cache expiration time
DTo change the backend server IP address
Attempts:
2 left
💡 Hint
Think about what the cache key represents in caching.
❓ Configuration
advanced
2:00remaining
Select the correct proxy_cache_key to cache by host and URI only
Which proxy_cache_key configuration caches responses uniquely by host and URI, ignoring query parameters?
Aproxy_cache_key "$host$uri";
Bproxy_cache_key "$host$request_uri";
Cproxy_cache_key "$host$args";
Dproxy_cache_key "$uri$args";
Attempts:
2 left
💡 Hint
$uri excludes query parameters, $request_uri includes them.
❓ Troubleshoot
advanced
2:30remaining
Why does nginx cache miss occur despite identical URLs?
You configured proxy_cache_key as "$host$request_uri" but see cache misses for requests with same URL but different 'User-Agent' headers. What is the likely cause?
AThe proxy_cache_key syntax is invalid causing cache misses
Bnginx ignores proxy_cache_key and caches by IP address
CThe backend server varies response by 'User-Agent', causing different content despite same cache key
DThe cache key does not include 'User-Agent', so cache misses are unexpected
Attempts:
2 left
💡 Hint
Consider how backend response variations affect caching.
✅ Best Practice
expert
3:00remaining
Best practice for proxy_cache_key with dynamic query parameters
You want to cache responses but ignore certain query parameters like 'session' and 'tracking' in the cache key. Which approach is best?
ADisable caching for requests with query parameters
BSet proxy_cache_key to "$host$request_uri" to include all query parameters
CUse proxy_cache_key "$host$uri$args" without filtering
DUse a map to filter unwanted query parameters and build proxy_cache_key with filtered args
Attempts:
2 left
💡 Hint
Think about how to exclude specific query parameters from cache key.

Practice

(1/5)
1. What is the main purpose of the proxy_cache_key directive in nginx?
easy
A. To define how nginx identifies and stores cached content
B. To set the maximum size of the cache
C. To specify the cache expiration time
D. To enable SSL for proxy connections

Solution

  1. Step 1: Understand the role of proxy_cache_key

    The proxy_cache_key tells nginx how to uniquely identify cached content by combining variables like scheme, host, and URI.
  2. Step 2: Differentiate from other cache settings

    Other directives control cache size or expiration, but proxy_cache_key specifically defines the cache lookup key.
  3. Final Answer:

    To define how nginx identifies and stores cached content -> Option A
  4. Quick Check:

    proxy_cache_key = cache identifier [OK]
Hint: proxy_cache_key sets cache identity, not size or time [OK]
Common Mistakes:
  • Confusing cache key with cache size or expiration
  • Thinking it enables SSL
  • Assuming it controls cache storage location
2. Which of the following is the correct syntax to set a proxy cache key using scheme, host, and request URI in nginx?
easy
A. proxy_cache_key $scheme$host$request_uri;
B. proxy_cache_key $scheme://$host$request_uri;
C. proxy_cache_key $scheme://host$request_uri;
D. proxy_cache_key "$scheme://$host$request_uri";

Solution

  1. Step 1: Check syntax for proxy_cache_key

    The value must be a string, so it should be enclosed in double quotes if it contains special characters like ://.
  2. Step 2: Verify variable usage

    Variables like $scheme, $host, and $request_uri must be concatenated properly inside quotes.
  3. Final Answer:

    proxy_cache_key "$scheme://$host$request_uri"; -> Option D
  4. Quick Check:

    Quotes needed for complex keys = proxy_cache_key "$scheme://$host$request_uri"; [OK]
Hint: Use quotes when combining variables with symbols like :// [OK]
Common Mistakes:
  • Omitting quotes around the key string
  • Missing $ before variables
  • Incorrect concatenation without quotes
3. Given this nginx config snippet:
proxy_cache_key "$scheme://$host$request_uri";

What will be the cache key for a request to https://example.com/path/page?query=1?
medium
A. example.com/path/page?query=1
B. https://example.com/path/page?query=1
C. https://example.com/path/page
D. http://example.com/path/page?query=1

Solution

  1. Step 1: Substitute variables with request values

    $scheme is 'https', $host is 'example.com', and $request_uri includes path and query: '/path/page?query=1'.
  2. Step 2: Combine variables as per the key format

    Joining them gives 'https://example.com/path/page?query=1'.
  3. Final Answer:

    https://example.com/path/page?query=1 -> Option B
  4. Quick Check:

    Variables combined exactly = https://example.com/path/page?query=1 [OK]
Hint: Remember $request_uri includes query string [OK]
Common Mistakes:
  • Ignoring the query string in $request_uri
  • Using http instead of https for scheme
  • Leaving out the scheme or host
4. You set proxy_cache_key "$scheme://$host$uri"; but nginx caches different query strings as the same content. What is the likely cause?
medium
A. You did not enable proxy_cache_path
B. You used $request_uri which already includes query string
C. You forgot to include $args in the cache key
D. You used double quotes instead of single quotes

Solution

  1. Step 1: Understand $uri vs $request_uri

    $uri contains only the path (normalized), excluding the query string. $request_uri includes the query.
  2. Step 2: Analyze the symptom

    Different query strings yield the same $uri, hence same cache key, causing shared cache content.
  3. Final Answer:

    You forgot to include $args in the cache key -> Option C
  4. Quick Check:

    $uri excludes query; add $args [OK]
Hint: $uri lacks query string; add $args [OK]
Common Mistakes:
  • Confusing cache key with cache size or expiration
  • Thinking it enables SSL
  • Assuming it controls cache storage location
5. You want to cache content separately for mobile and desktop users. Which proxy cache key configuration achieves this if user agent contains 'Mobile' for mobile devices?
hard
A. proxy_cache_key "$scheme://$host$request_uri:$is_mobile"; # where $is_mobile is set by map
B. proxy_cache_key "$scheme://$host$request_uri:$http_user_agent ~* Mobile";
C. proxy_cache_key "$scheme://$host$request_uri:$http_user_agent";
D. proxy_cache_key "$scheme://$host$request_uri";

Solution

  1. Step 1: Understand the need for conditional cache key

    We want to separate cache for mobile and desktop, so the key must include a variable indicating device type.
  2. Step 2: Use a map to set $is_mobile variable

    nginx can use map to set $is_mobile to 'mobile' or 'desktop' based on $http_user_agent matching 'Mobile'. Including $is_mobile in cache key separates caches.
  3. Step 3: Evaluate options

    proxy_cache_key "$scheme://$host$request_uri:$http_user_agent"; uses full user agent, which is too specific and causes cache fragmentation. proxy_cache_key "$scheme://$host$request_uri:$http_user_agent ~* Mobile"; is invalid syntax. proxy_cache_key "$scheme://$host$request_uri"; ignores device type. proxy_cache_key "$scheme://$host$request_uri:$is_mobile"; # where $is_mobile is set by map correctly uses $is_mobile set by map.
  4. Final Answer:

    proxy_cache_key "$scheme://$host$request_uri:$is_mobile"; # where $is_mobile is set by map -> Option A
  5. Quick Check:

    Use map variable for device type in cache key = proxy_cache_key "$scheme://$host$request_uri:$is_mobile"; # where $is_mobile is set by map [OK]
Hint: Use map to create $is_mobile variable for cache key [OK]
Common Mistakes:
  • Using full user agent causing too many cache entries
  • Trying regex inside proxy_cache_key (invalid)
  • Ignoring device type in cache key
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