Bird
Raised Fist0
Nginxdevops~5 mins

Proxy cache basics in Nginx - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes web servers get too many requests and slow down. Proxy cache helps by saving copies of web pages so future visitors get them faster without asking the main server again.
When your website gets many visitors asking for the same pages repeatedly.
When you want to reduce the load on your main web server to keep it fast.
When you want to speed up response times for users by serving cached content.
When you have a backend server that is slow or expensive to run for every request.
When you want to save bandwidth by not fetching the same data multiple times.
Config File - nginx.conf
nginx.conf
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=60m use_temp_path=off;

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale error timeout updating;
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}

proxy_cache_path defines where cached files are stored, cache size, and cache zone name.

server block listens on port 80 for example.com.

location / proxies requests to backend_server and enables caching using the defined cache zone.

proxy_cache_valid sets how long responses with certain status codes are cached.

proxy_cache_use_stale allows serving stale cache if backend has errors or timeouts.

add_header adds a header to show cache status for debugging.

Commands
Check the nginx configuration file syntax to make sure there are no errors before restarting.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart nginx to apply the new proxy cache configuration.
Terminal
sudo systemctl restart nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the server and check the response headers to see if the cache is working (look for X-Proxy-Cache header).
Terminal
curl -I http://example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Tue, 27 Jun 2024 12:00:00 GMT Content-Type: text/html X-Proxy-Cache: MISS Connection: keep-alive
Send the same request again to see if the response is served from cache (X-Proxy-Cache should show HIT).
Terminal
curl -I http://example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Tue, 27 Jun 2024 12:00:10 GMT Content-Type: text/html X-Proxy-Cache: HIT Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: proxy cache stores copies of responses to serve future requests faster and reduce backend load.

Common Mistakes
Not testing nginx configuration before restart.
This can cause nginx to fail restarting due to syntax errors, making the website unavailable.
Always run 'nginx -t' to check configuration syntax before restarting.
Forgetting to set proxy_cache_path before using proxy_cache.
Without defining proxy_cache_path, nginx will not know where to store cache and caching won't work.
Define proxy_cache_path with proper parameters before enabling proxy_cache in location.
Not checking cache status headers to verify caching.
You won't know if caching is working or not, leading to confusion and troubleshooting delays.
Add a header like X-Proxy-Cache and check it with curl to confirm cache hits or misses.
Summary
Define proxy_cache_path to set cache storage location and size.
Enable proxy_cache in server location to cache backend responses.
Use curl and headers to verify if caching is working as expected.

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

  1. Step 1: Understand proxy_cache role

    The proxy_cache directive tells nginx to save copies of responses from the backend server.
  2. Step 2: Identify purpose of caching

    Storing these copies helps nginx deliver content faster to users without asking the backend every time.
  3. Final Answer:

    To store copies of web responses for faster delivery -> Option A
  4. 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

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

    proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; -> Option B
  4. 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]
Common Mistakes:
  • Placing keys_zone before path
  • Missing semicolon at end
  • Wrong order of parameters
3. Given this nginx config snippet:
proxy_cache_path /tmp/cache keys_zone=mycache:10m;
server {
  location / {
    proxy_cache mycache;
    proxy_cache_valid 200 10m;
    proxy_pass http://backend;
  }
}

What happens when a client requests a page for the first time?
medium
A. nginx fetches from backend and caches the response
B. nginx serves the cached page immediately
C. nginx returns an error because cache is empty
D. nginx ignores cache and always fetches from backend

Solution

  1. Step 1: Understand first request behavior

    On the first request, cache is empty, so nginx must get the response from the backend server.
  2. Step 2: Recognize caching after fetch

    After fetching, nginx stores the response in cache for future requests within 10 minutes.
  3. Final Answer:

    nginx fetches from backend and caches the response -> Option A
  4. Quick Check:

    First request = fetch then cache [OK]
Hint: First request fetches backend, then caches response [OK]
Common Mistakes:
  • Assuming cache serves before first fetch
  • Thinking nginx errors if cache empty
  • Believing cache is ignored always
4. You set proxy_cache_valid 200 5m; but cached responses are never used. What is a likely cause?
medium
A. The keys_zone size is too small
B. The cache path directory does not exist
C. The backend server returns status 404 instead of 200
D. The proxy_cache directive is missing in the location block

Solution

  1. Step 1: Check proxy_cache usage

    Without proxy_cache enabled in the location, caching won't happen even if proxy_cache_valid is set.
  2. Step 2: Understand effect of missing proxy_cache

    Missing this directive means nginx never stores or serves cached responses, causing cache to be unused.
  3. Final Answer:

    The proxy_cache directive is missing in the location block -> Option D
  4. Quick Check:

    Missing proxy_cache disables caching [OK]
Hint: proxy_cache must be set to enable caching [OK]
Common Mistakes:
  • Assuming cache path missing causes no cache use
  • Thinking backend status unrelated
  • Believing keys_zone size stops caching
5. You want nginx to cache responses with status 200 for 10 minutes and status 404 for 1 minute. Which config snippet achieves this?
hard
A. proxy_cache_valid 200 10m, 404 1m;
B. proxy_cache_valid 10m 200; proxy_cache_valid 1m 404;
C. proxy_cache_valid 200 10m; proxy_cache_valid 404 1m;
D. ;m1 404 ,m01 002 dilav_ehcac_yxorp

Solution

  1. Step 1: Recall proxy_cache_valid syntax

    The correct syntax is proxy_cache_valid [status] [time]; for each status separately.
  2. Step 2: Identify correct multiple directives

    Using two separate proxy_cache_valid lines for 200 and 404 with their times is valid and clear.
  3. Final Answer:

    proxy_cache_valid 200 10m; proxy_cache_valid 404 1m; -> Option C
  4. Quick Check:

    Separate lines for each status = proxy_cache_valid 200 10m; proxy_cache_valid 404 1m; [OK]
Hint: Use separate proxy_cache_valid lines per status [OK]
Common Mistakes:
  • Combining statuses in one line incorrectly
  • Using commas instead of semicolons
  • Reversing time and status order
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