Bird
Raised Fist0
Nginxdevops~10 mins

Proxy cache path configuration in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Proxy cache path configuration
Define cache path with 'proxy_cache_path'
↓
Set cache zone name and size
↓
Configure cache keys and inactive time
↓
Use 'proxy_cache' in server/location block
↓
Cache stores responses in defined path
↓
Serve cached content on repeated requests
↓
Cache expires or is purged based on rules
This flow shows how nginx sets up a cache directory, assigns a cache zone, and then uses it to store and serve cached responses.
Execution Sample
Nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mycache:10m max_size=1g inactive=60m;

server {
  location / {
    proxy_cache mycache;
    proxy_pass http://backend;
  }
}
This config sets a cache directory and zone, then enables caching for requests to the backend server.
Process Table
StepDirectiveActionResult
1proxy_cache_pathDefine cache path '/var/cache/nginx' with zone 'mycache'Cache zone 'mycache' created with 10MB memory, max size 1GB
2levels=1:2Set cache directory structureCache files stored in 2-level subdirectories for efficiency
3inactive=60mSet inactive timeoutCache entries expire if not accessed for 60 minutes
4proxy_cache mycacheEnable caching in location blockRequests will use 'mycache' zone for caching
5proxy_pass http://backendForward request to backendResponse received and stored in cache
6Repeat requestServe cached response if validFaster response served from cache
7Cache expires or purgedOld cache entries removedCache space freed for new entries
💡 Cache path and zone configured; caching enabled and serving cached content until cache expires or is purged
Status Tracker
VariableStartAfter Step 1After Step 4After Step 6Final
cache_pathundefined/var/cache/nginx/var/cache/nginx/var/cache/nginx/var/cache/nginx
cache_zoneundefinedmycache (10m)mycache (10m)mycache (10m)mycache (10m)
cache_filesemptyemptyemptycontains cached responsescontains cached responses or expired entries removed
Key Moments - 3 Insights
Why do we specify 'levels=1:2' in proxy_cache_path?
This creates a two-level directory structure inside the cache path to store cached files efficiently and avoid too many files in one folder, as shown in step 2 of the execution_table.
What happens if a cached response is not accessed for 60 minutes?
It expires and is removed from the cache, as set by 'inactive=60m' in step 3, freeing space for new cache entries.
How does nginx know to use the cache for requests?
By using 'proxy_cache mycache;' inside the location block (step 4), nginx enables caching for requests matching that location.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache zone size defined at step 1?
A1 gigabyte
B60 minutes
C10 megabytes
D2 levels
💡 Hint
Check the 'Result' column in step 1 for cache zone memory size.
At which step does nginx start serving cached responses?
AStep 3
BStep 6
CStep 5
DStep 7
💡 Hint
Look for when cached responses are served in the 'Action' column.
If we remove 'inactive=60m', what changes in the cache behavior?
ACache entries expire after the default 10 minutes of inactivity
BCache directory structure changes
CCache zone size increases
DCaching is disabled
💡 Hint
Refer to step 3 about inactive timeout and its effect on cache expiration.
Concept Snapshot
proxy_cache_path sets cache directory and zone with size and structure
Use 'proxy_cache zone_name;' in location to enable caching
Cache stores responses on disk in subfolders
'inactive' sets how long unused cache stays
Cached responses speed up repeated requests
Expired cache entries are removed automatically
Full Transcript
This visual execution shows how nginx configures proxy cache path using 'proxy_cache_path' directive. First, it defines the cache directory and zone name with memory size and max disk size. Then it sets directory levels for storing cache files efficiently. The 'inactive' parameter sets how long cached items stay if not accessed. In the server block, 'proxy_cache' enables caching for requests. When a request is proxied, the response is stored in the cache path. Subsequent requests serve the cached response for faster delivery. Cache entries expire or are purged based on the inactive timeout or other rules. Variables like cache_path and cache_zone track the configuration state. Key moments include understanding directory levels, cache expiration, and enabling cache in location. The quiz tests knowledge of cache size, serving cached content, and effect of inactive timeout.

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

  1. 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.
  2. Step 2: Compare with other options

    Options A, C, and D relate to connections, SSL, and backend servers, not caching.
  3. Final Answer:

    To define where and how cached files are stored -> Option D
  4. Quick Check:

    proxy_cache_path = cache storage location [OK]
Hint: proxy_cache_path sets cache storage location [OK]
Common Mistakes:
  • Confusing proxy_cache_path with proxy_pass
  • Thinking it sets connection limits
  • Mixing it with SSL configuration
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

  1. 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.
  2. 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.
  3. Final Answer:

    proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m; -> Option A
  4. 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]
Common Mistakes:
  • Missing semicolon at the end
  • Using space instead of colon in keys_zone
  • Omitting equal signs in parameters
3. Given this nginx configuration snippet:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mycache:10m max_size=500m inactive=30m;

What happens when a cached file is not accessed for 40 minutes?
medium
A. The cached file is refreshed automatically
B. The cached file remains because max_size is 500m
C. The cached file is removed because it is inactive longer than 30 minutes
D. The cached file is moved to a different cache zone

Solution

  1. Step 1: Understand the inactive parameter

    The inactive=30m means cached files not accessed for 30 minutes are removed.
  2. Step 2: Apply to 40 minutes inactivity

    Since 40 minutes > 30 minutes, the cached file will be removed.
  3. Final Answer:

    The cached file is removed because it is inactive longer than 30 minutes -> Option C
  4. Quick Check:

    inactive=30m means remove after 30 minutes inactivity [OK]
Hint: inactive time removes unused cache files after set duration [OK]
Common Mistakes:
  • Confusing max_size with inactive time
  • Assuming cache refresh happens automatically
  • Thinking cache files move zones
4. You wrote this proxy cache path configuration:
proxy_cache_path /cache keys_zone=mycache 10m max_size=1g inactive=60m;

nginx fails to start. What is the error?
medium
A. Missing colon between keys_zone name and size
B. Invalid max_size value
C. Path /cache does not exist
D. inactive parameter should be in seconds

Solution

  1. Step 1: Check keys_zone syntax

    The keys_zone requires a colon between the zone name and size, e.g., mycache:10m.
  2. Step 2: Identify missing colon

    The config uses keys_zone=mycache 10m missing the colon, causing syntax error.
  3. Final Answer:

    Missing colon between keys_zone name and size -> Option A
  4. 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

  1. Step 1: Verify keys_zone size and path

    Keys zone size is 20m and path is /var/cache/nginx, both correct in all options.
  2. 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.
  3. 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').
  4. Final Answer:

    proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=15m; -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard