📖 Scenario: You are setting up a simple web server using Nginx. To improve performance and reduce load on the backend, you want to configure a proxy cache. This cache will store responses temporarily on disk so that repeated requests can be served faster.
🎯 Goal: Configure Nginx to use a proxy cache with a specific cache path on disk. You will create the cache path, set cache parameters, and then enable proxy caching for a backend server.
📋 What You'll Learn
Create a proxy cache path with a defined directory and cache parameters
Define a cache zone with a name and size
Configure a server block to use the proxy cache for requests
Print the final Nginx configuration snippet
💡 Why This Matters
🌍 Real World
Proxy caching in Nginx helps speed up websites by storing responses on disk, reducing backend load and improving user experience.
💼 Career
Understanding proxy cache configuration is essential for DevOps roles managing web servers and optimizing application performance.
Progress0 / 4 steps
1
Create the proxy cache path
Write a line to create a proxy_cache_path directive with the path /var/cache/nginx, a cache zone named my_cache with size 10m, and keys zone my_cache:10m. Include inactive=60m and max_size=100m parameters.
Nginx
Hint
Use the proxy_cache_path directive with the exact path and parameters.
2
Define the proxy cache zone
Add a line to define a proxy_cache variable named my_cache inside an http block.
Nginx
Hint
Inside the http block, set proxy_cache my_cache; to use the cache zone.
3
Configure server block to use proxy cache
Inside the http block, add a server block listening on port 80. Inside it, add a location / block that proxies requests to http://backend_server and enables proxy caching using proxy_cache my_cache;.
Nginx
Hint
Define a server block with listen 80; and a location / that proxies to http://backend_server and uses the cache.
4
Print the final Nginx configuration
Print the full Nginx configuration you wrote so far.
Nginx
Hint
Use a print statement to output the full configuration exactly as written.
Practice
(1/5)
1. What is the main purpose of the proxy_cache_path directive in nginx?
easy
A. To set the maximum number of client connections
B. To specify the backend server IP address
C. To configure SSL certificates for secure connections
D. To define where and how cached files are stored
Solution
Step 1: Understand the role of proxy_cache_path
The proxy_cache_path directive tells nginx where to save cached files and how to manage them.
Step 2: Compare with other options
Options A, C, and D relate to connections, SSL, and backend servers, not caching.
Final Answer:
To define where and how cached files are stored -> Option D
2. Which of the following is the correct syntax to define a proxy cache path in nginx?
easy
A. proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m;
B. proxy_cache_path /data/cache keys_zone=mycache 10m max_size=1g inactive=60m
C. proxy_cache_path /data/cache keys_zone mycache:10m max_size=1g inactive=60m;
D. proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m
Solution
Step 1: Check syntax requirements for proxy_cache_path
The directive requires a semicolon at the end and correct key-value pairs with colons where needed.
Step 2: Identify correct option
proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m; has correct syntax with colon in keys_zone, key-value pairs, and ends with a semicolon.
Final Answer:
proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m; -> Option A
Quick Check:
Correct syntax ends with semicolon and uses colons properly [OK]
Hint: Always end proxy_cache_path with semicolon and use colons in keys_zone [OK]
The keys_zone requires a colon between the zone name and size, e.g., mycache:10m.
Step 2: Identify missing colon
The config uses keys_zone=mycache 10m missing the colon, causing syntax error.
Final Answer:
Missing colon between keys_zone name and size -> Option A
Quick Check:
keys_zone syntax requires colon like mycache:10m [OK]
Hint: keys_zone must have colon between name and size [OK]
Common Mistakes:
Forgetting colon in keys_zone
Assuming inactive needs seconds
Ignoring path existence errors
5. You want to configure nginx to cache responses in /var/cache/nginx with a 20MB keys zone, max cache size 2GB, and remove files inactive for 15 minutes. Which is the correct proxy_cache_path directive?
hard
A. proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2gb inactive=15m;
B. proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=15m;
C. proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=1024m inactive=15m;
D. proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=900;
Solution
Step 1: Verify keys_zone size and path
Keys zone size is 20m and path is /var/cache/nginx, both correct in all options.
Step 2: Check max_size and inactive syntax
Max size should be 2g (gigabytes), inactive time 15m (minutes). proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=15m; uses correct units and syntax.
Step 3: Identify errors in other options
proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2gb inactive=15m; uses '2gb' (invalid unit), C uses 1024m (1GB not 2GB), D uses inactive=900 (seconds, but default is minutes and needs 's').
Final Answer:
proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=15m; -> Option B
Quick Check:
Use correct units: m for MB, g for GB, and time units with m for minutes [OK]
Hint: Use correct units: m for MB, g for GB, and m for minutes [OK]
Common Mistakes:
Using invalid units like 'gb' instead of 'g'
Confusing inactive time units
Using seconds without 's' suffix
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.