0
0
Nginxdevops~10 mins

Proxy cache basics in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Proxy cache basics
Client sends request
Nginx receives request
Check cache for response
Serve cached
Return response
Return response
Nginx checks if a cached response exists for a client request. If yes, it serves the cached response. If no, it fetches from the upstream server, caches it, then returns it.
Execution Sample
Nginx
proxy_cache_path /tmp/cache keys_zone=mycache:10m;
server {
  location / {
    proxy_cache mycache;
    proxy_pass http://backend;
  }
}
This config sets up a cache zone and enables proxy caching for requests to the backend server.
Process Table
StepActionCache CheckCache Hit?Result
1Client sends request for /index.htmlN/AN/ARequest received by Nginx
2Nginx checks cache for /index.htmlLook in /tmp/cacheNoCache miss, fetch from backend
3Nginx requests /index.html from backendN/AN/ABackend responds with content
4Nginx stores response in cacheWrite to /tmp/cacheN/AResponse cached
5Nginx returns response to clientN/AN/AClient receives fresh content
6Client sends request for /index.html againN/AN/ARequest received by Nginx
7Nginx checks cache for /index.htmlLook in /tmp/cacheYesCache hit, serve cached content
8Nginx returns cached response to clientN/AN/AClient receives cached content
💡 Execution stops after serving cached content on second request.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 7Final
cache for /index.htmlemptymissstoredhithit
Key Moments - 3 Insights
Why does Nginx fetch from backend on the first request?
Because the cache is empty at first (see execution_table step 2 shows cache miss), so Nginx must get fresh content.
How does Nginx know when to serve cached content?
It checks the cache for the requested URL (step 7 shows cache hit), then serves cached content without contacting backend.
What happens after Nginx fetches content from backend?
Nginx stores the response in cache (step 4) so future requests can be served faster.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache status at Step 2?
ACache miss
BCache hit
CCache storing
DCache empty
💡 Hint
Check the 'Cache Hit?' column at Step 2 in execution_table.
At which step does Nginx store the response in cache?
AStep 3
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the action 'stores response in cache' in execution_table.
If the cache was never stored, what would happen at Step 7?
AReturn error
BCache hit
CCache miss
DServe stale content
💡 Hint
Refer to variable_tracker for cache state after Step 4 and Step 7.
Concept Snapshot
proxy_cache_path sets cache location and size.
proxy_cache enables caching in location.
Nginx checks cache before fetching upstream.
Cache hit serves cached content fast.
Cache miss fetches and stores response.
Improves performance and reduces backend load.
Full Transcript
Proxy cache basics in Nginx means saving responses from backend servers so future requests can be served quickly from cache. When a client sends a request, Nginx first looks in its cache. If the response is there (cache hit), it sends it immediately. If not (cache miss), Nginx fetches the response from the backend, stores it in cache, then returns it to the client. This process speeds up responses and reduces backend work. The example config shows how to set up a cache zone and enable proxy caching. The execution table traces two requests: the first misses cache and stores response; the second hits cache and serves cached content. Variables track cache state changes. Key moments clarify why cache miss happens first and how cache hit works later. The quiz tests understanding of cache status and steps. The snapshot summarizes the main points for quick recall.