📖 Scenario: You are setting up a simple web server proxy using nginx. To improve performance, you want to add caching so that repeated requests for the same content are served faster.
🎯 Goal: Build a basic nginx proxy cache configuration that caches responses from a backend server and serves cached content for repeated requests.
📋 What You'll Learn
Create a cache path configuration with a specific name and location
Define a proxy cache zone with a given name
Configure a server block to use the proxy cache for requests
Print the final nginx configuration to verify the setup
💡 Why This Matters
🌍 Real World
Proxy caching is used in web servers to speed up response times by storing copies of backend responses. This reduces load on backend servers and improves user experience.
💼 Career
Understanding proxy cache basics is important for DevOps engineers and system administrators who manage web infrastructure and optimize performance.
Progress0 / 4 steps
1
Create cache path configuration
Create a cache path configuration using proxy_cache_path with the path /var/cache/nginx, a cache zone named my_cache with 10 megabytes of storage, and set inactive=60m.
Nginx
Hint
Use proxy_cache_path directive with the exact path, zone name, size, and inactive time.
2
Add proxy cache to server block
Add a server block with listen 80; and location / that proxies requests to http://backend_server. Inside the location block, add the directive proxy_cache my_cache; to enable caching using the cache zone my_cache.
Nginx
Hint
Define a server block with listen 80; and a location / block that includes proxy_pass and proxy_cache directives.
3
Add proxy cache key and valid time
Inside the location / block, add the directive proxy_cache_key "$scheme$request_method$host$request_uri"; to define the cache key. Also add proxy_cache_valid 200 302 10m; to cache successful responses for 10 minutes.
Nginx
Hint
Use proxy_cache_key with the exact string and proxy_cache_valid for status codes 200 and 302 with 10 minutes duration.
4
Print the final nginx configuration
Print the entire nginx configuration by writing print(config) where config is a string variable containing the full configuration from previous steps.
Nginx
Hint
Use print(config) to display the full nginx configuration string.
Practice
(1/5)
1. What is the main purpose of proxy_cache in nginx?
easy
A. To store copies of web responses for faster delivery
B. To block unwanted web requests
C. To encrypt web traffic
D. To balance load between servers
Solution
Step 1: Understand proxy_cache role
The proxy_cache directive tells nginx to save copies of responses from the backend server.
Step 2: Identify purpose of caching
Storing these copies helps nginx deliver content faster to users without asking the backend every time.
Final Answer:
To store copies of web responses for faster delivery -> Option A
Quick Check:
proxy_cache = store copies for speed [OK]
Hint: proxy_cache saves responses to speed up delivery [OK]
Common Mistakes:
Confusing proxy_cache with load balancing
Thinking proxy_cache encrypts traffic
Assuming proxy_cache blocks requests
2. Which of the following is the correct syntax to define a cache path in nginx?
easy
A. proxy_cache_path /data/nginx/cache keys_zone=mycache:10m levels=1:2
B. proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m;
C. proxy_cache_path keys_zone=mycache:10m /data/nginx/cache levels=1:2;
D. proxy_cache_path levels=1:2 keys_zone=mycache:10m /data/nginx/cache;
Solution
Step 1: Recall correct order of proxy_cache_path
The correct syntax starts with the cache directory path, then options like levels and keys_zone.
Step 2: Match syntax with options order
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; correctly places the path first, followed by levels and keys_zone with proper separators and semicolon.
Final Answer:
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; -> Option B
Quick Check:
Path first, then options = proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; [OK]
Hint: Cache path starts with directory, then options [OK]