📖 Scenario: You manage a website using Nginx as a reverse proxy with caching enabled. Sometimes, you need to clear specific cached content so visitors see the latest updates immediately.
🎯 Goal: Learn how to configure Nginx to allow purging of cached content and test purging a specific cache key.
📋 What You'll Learn
Create a cache zone named my_cache with a 10MB size
Configure a location /purge/ to allow cache purging using the PURGE HTTP method
Write a command to purge the cache for the URL /index.html
Show the output of the purge command
💡 Why This Matters
🌍 Real World
Websites often cache content to speed up delivery. When content changes, purging cache ensures visitors see fresh content immediately.
💼 Career
DevOps engineers and system administrators use cache purging to maintain website performance and content accuracy.
Progress0 / 4 steps
1
Create a cache zone in Nginx
Write the proxy_cache_path directive to create a cache zone named my_cache with a 10MB size and store cache files in /var/cache/nginx.
Nginx
Hint
Use proxy_cache_path with keys_zone=my_cache:10m and max_size=10m.
2
Configure purge location for cache
Add a location /purge/ block that allows the PURGE HTTP method and uses proxy_cache_purge my_cache to purge cached content.
Nginx
Hint
Use location /purge/ { ... } with proxy_cache_purge my_cache; and allow only localhost to purge.
3
Write the purge command
Write the curl command to send a PURGE request to http://localhost/purge/index.html to clear the cached /index.html content.
Nginx
Hint
Use curl -X PURGE http://localhost/purge/index.html to purge the cache.
4
Show the purge command output
Run the purge command and print the output. The expected output is HTTP/1.1 200 OK indicating the cache was cleared.
Nginx
Hint
The purge command should return HTTP/1.1 200 OK if successful.
Practice
(1/5)
1. What is the main purpose of purging cached content in nginx?
easy
A. To remove specific cached files so fresh content is served
B. To increase the cache size automatically
C. To disable caching completely
D. To restart the nginx server
Solution
Step 1: Understand caching in nginx
Caching stores copies of content to serve faster responses.
Step 2: Understand purging purpose
Purging removes specific cached files to update content without restarting nginx.
Final Answer:
To remove specific cached files so fresh content is served -> Option A
Quick Check:
Purging = Remove cached files [OK]
Hint: Purging means deleting cached files to refresh content [OK]
Common Mistakes:
Confusing purging with disabling cache
Thinking purging restarts nginx
Assuming purging increases cache size
2. Which of the following is the correct syntax to enable cache purging in an nginx location block?
easy
A. proxy_cache_purge true;
B. proxy_cache_purge off;
C. proxy_cache_purge enable;
D. proxy_cache_purge mycache;
Solution
Step 1: Recall nginx directive syntax
Directives use the format: directive_name value; with zone name for cache purging.
Step 2: Identify correct value for enabling purge
Enabling uses zone name 'mycache' not 'enable', 'true', or 'off'.
Final Answer:
proxy_cache_purge mycache; -> Option D
Quick Check:
Enable purge = proxy_cache_purge mycache; [OK]
Hint: Use the zone name to enable cache purging in nginx [OK]
But PURGE requests still return 403 Forbidden. What is the likely cause?
medium
A. Cache is empty so purge fails
B. proxy_cache_purge directive is misspelled
C. Missing allow/deny rules to permit purge requests
D. PURGE method is not supported by nginx
Solution
Step 1: Understand default access control
Without 'allow', nginx denies all due to 'deny all;' for security.
Step 2: Check purge access rules
To allow PURGE requests, you must explicitly allow trusted IPs.
Final Answer:
Missing allow/deny rules to permit purge requests -> Option C
Quick Check:
deny all without allow = 403 Forbidden on PURGE [OK]
Hint: Always add allow/deny for purge locations [OK]
Common Mistakes:
Assuming PURGE method is unsupported
Ignoring access control rules
Thinking empty cache causes 403
5. You want to purge cached content only for URLs starting with /images/ and restrict purge access to IP 10.0.0.5. Which nginx config snippet correctly achieves this?