0
0
Nginxdevops~10 mins

Cache-Control headers in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Cache-Control headers
Client Request
nginx receives request
Check Cache-Control config
Add Cache-Control header to response
Send response with Cache-Control header
Client/browser caches or not based on header
The flow shows how nginx adds Cache-Control headers to HTTP responses, guiding browsers on caching behavior.
Execution Sample
Nginx
location /static/ {
    add_header Cache-Control "public, max-age=3600";
}
This nginx config adds a Cache-Control header to responses for /static/ files, allowing caching for 1 hour.
Process Table
StepActionCache-Control Header SetEffect on Client Cache
1Client requests /static/image.pngNone yetNo cache info yet
2nginx matches location /static/None yetNo cache info yet
3nginx adds header: Cache-Control: public, max-age=3600public, max-age=3600Client can cache for 3600 seconds
4Response sent to client with headerpublic, max-age=3600Client stores response in cache
5Client requests same resource within 3600spublic, max-age=3600Client serves from cache, no new request sent
6Client requests after 3600spublic, max-age=3600Client sends new request to server
7Execution endsN/ACache expired, new fetch needed
💡 Cache expires after max-age seconds, client must revalidate or fetch new response
Status Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
Cache-Control HeaderNonepublic, max-age=3600public, max-age=3600public, max-age=3600public, max-age=3600N/A
Client Cache StateEmptyEmptyCached response storedServing from cacheCache expiredEmpty
Key Moments - 3 Insights
Why does the client serve the cached response without contacting the server between steps 4 and 5?
Because the Cache-Control header with max-age=3600 tells the client the response is fresh for 3600 seconds, so it can use the cached copy without asking the server again (see execution_table step 5).
What happens when the max-age time expires?
After max-age seconds, the client considers the cached response stale and sends a new request to the server to get fresh content (see execution_table step 6).
Does nginx cache the response itself in this example?
No, nginx only adds the Cache-Control header. The caching happens on the client/browser side based on that header.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What Cache-Control header does nginx add?
Ano-cache
Bprivate, max-age=0
Cpublic, max-age=3600
Dno-store
💡 Hint
Check the 'Cache-Control Header Set' column at step 3 in the execution_table.
At which step does the client start serving the response from its cache?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Effect on Client Cache' column to find when the client uses cached data.
If max-age was set to 0, how would the execution_table change at step 5?
AClient would send a new request at step 5
BCache-Control header would be missing
CClient would serve from cache at step 5
Dnginx would not add any header
💡 Hint
max-age=0 means no caching; check client cache behavior in variable_tracker.
Concept Snapshot
Cache-Control headers in nginx:
Use add_header directive inside location or server block.
Example: add_header Cache-Control "public, max-age=3600";
Controls how browsers cache responses.
max-age sets seconds to keep cached copy.
public means cacheable by any cache.
Full Transcript
This visual execution shows how nginx adds Cache-Control headers to HTTP responses. When a client requests a resource under /static/, nginx adds the header 'Cache-Control: public, max-age=3600'. This tells the client to cache the response for 3600 seconds. The client stores the response and serves it from cache for any requests within that time, avoiding new server requests. After 3600 seconds, the client considers the cache expired and requests the resource again from the server. nginx itself does not cache the response here; it only instructs the client how to cache. This flow helps improve performance by reducing repeated server requests for static content.