📖 Scenario: You manage a busy website that serves dynamic content. To improve speed and reduce server load, you want to add a short cache (micro-cache) for dynamic pages using nginx. This cache will store responses for a few seconds so repeated requests are faster.
🎯 Goal: Set up a micro-cache in nginx that caches dynamic content for 5 seconds. You will create the cache zone, configure caching rules, and verify the cache works by printing cache status headers.
📋 What You'll Learn
Create an nginx cache zone named microcache with 10MB size
Set cache key to $scheme$host$request_uri
Cache dynamic content for 5 seconds
Add headers to show cache status
Test caching by printing cache status header
💡 Why This Matters
🌍 Real World
Micro-caching helps busy websites serve dynamic content faster by temporarily storing responses. This reduces server load and improves user experience.
💼 Career
DevOps engineers often configure nginx caching to optimize web performance and scalability in production environments.
Progress0 / 4 steps
1
Create the cache zone
In the http block of your nginx config, create a cache zone named microcache with 10MB size using proxy_cache_path. Use /tmp/nginx_microcache as the cache directory.
Nginx
Hint
Use proxy_cache_path directive inside http block to define cache zone.
2
Configure cache key and enable micro-caching
Inside the server block, set proxy_cache to microcache and define proxy_cache_key as $scheme$host$request_uri. Set proxy_cache_valid to cache 200 responses for 5 seconds.
Nginx
Hint
Use proxy_cache, proxy_cache_key, and proxy_cache_valid directives inside server block.
3
Add headers to show cache status
Inside the location / block, add proxy_pass to http://backend. Add add_header directives to show X-Cache-Status header with $upstream_cache_status value.
Nginx
Hint
Use location / block with proxy_pass and add_header directives.
4
Test and print cache status header
Run curl -i http://localhost/ to see the X-Cache-Status header in the response. This shows if the response was served from cache.
Nginx
Hint
Use curl -i http://localhost/ to see headers including X-Cache-Status.
Practice
(1/5)
1. What is the main purpose of micro-caching dynamic content in nginx?
easy
A. To temporarily store dynamic pages to reduce server load and improve response time
B. To permanently save static files on the server
C. To disable caching for all dynamic content
D. To increase the size of the server's memory
Solution
Step 1: Understand micro-caching concept
Micro-caching stores dynamic content briefly to reduce repeated processing and speed up responses.
Step 2: Identify the main benefit
This reduces server load and improves response time during traffic spikes or frequent requests.
Final Answer:
To temporarily store dynamic pages to reduce server load and improve response time -> Option A
What happens when a user requests /dynamic/page1 twice within 3 seconds?
medium
A. The second request is served from cache, speeding up response
B. The second request bypasses cache and fetches fresh content
C. Both requests cause backend to generate content
D. Cache is cleared after first request, so second fails
Solution
Step 1: Understand cache duration and behavior
Cache duration is 5 seconds, so content is cached for 5 seconds after first request.
Step 2: Analyze requests timing
Second request is within 3 seconds, so cache is still valid and serves cached content.
Final Answer:
The second request is served from cache, speeding up response -> Option A
Quick Check:
Cache valid 5s means repeated requests within 5s use cache [OK]
Hint: Requests within cache time get cached response [OK]
Common Mistakes:
Assuming cache is bypassed before expiry
Thinking cache clears after each request
Confusing cache duration units
4. You configured micro-caching with:
proxy_cache_valid 10s;
But cache never stores responses. What is a likely cause?
medium
A. The server block lacks listen directive
B. The cache duration is too short to store anything
C. The proxy_pass URL is invalid
D. The proxy_cache_path directive is missing or incorrect
Solution
Step 1: Check required cache setup directives
For caching to work, proxy_cache_path must define cache storage and zone.
Step 2: Identify missing or incorrect cache path
If proxy_cache_path is missing or wrong, cache cannot store responses despite valid duration.
Final Answer:
The proxy_cache_path directive is missing or incorrect -> Option D
Quick Check:
proxy_cache_path must be set for caching to work [OK]
Hint: Always define proxy_cache_path to enable caching [OK]
Common Mistakes:
Ignoring proxy_cache_path directive
Blaming cache duration length
Confusing proxy_pass errors with caching issues
5. You want to micro-cache dynamic API responses for 2 seconds but only cache successful responses (status 200). Which nginx config snippet achieves this?
hard
A. proxy_cache_valid 200 2s;
proxy_cache_valid 404 0s;
B. proxy_cache_valid 2s;
proxy_cache_use_stale error timeout;
C. proxy_cache_valid 200 2s;
proxy_cache_valid any 0s;
D. proxy_cache_valid 2s;
proxy_cache_min_uses 1;
Solution
Step 1: Understand proxy_cache_valid usage by status
To cache only status 200 responses for 2 seconds, use proxy_cache_valid 200 2s;.
Step 2: Prevent caching other statuses
Set proxy_cache_valid any 0s; to avoid caching other statuses like 404.
Final Answer:
proxy_cache_valid 200 2s;
proxy_cache_valid any 0s; -> Option C
Quick Check:
Cache only 200 status with proxy_cache_valid [OK]
Hint: Use proxy_cache_valid with status code to control caching [OK]
Common Mistakes:
Not specifying status code in proxy_cache_valid
Using invalid directive names
Caching error or 404 responses unintentionally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.