0
0
Nginxdevops~10 mins

Expires directive in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Expires directive
Client requests resource
Nginx checks Expires directive
Calculate expiration time
Add Expires header to response
Send response with Expires header
Client caches resource until expiration
After expiration, client requests resource again
This flow shows how nginx uses the Expires directive to tell browsers when a resource should be considered fresh or stale.
Execution Sample
Nginx
location /images/ {
    expires 30d;
}
This configuration tells nginx to add an Expires header to resources under /images/ that makes browsers cache them for 30 days.
Process Table
StepActionExpires Directive ValueExpires Header SentEffect on Client
1Client requests /images/logo.png30dExpires: current_time + 30 daysClient caches resource for 30 days
2Client requests /images/banner.jpg before 30 days30dExpires header still validClient uses cached resource, no new request sent
3Client requests /images/banner.jpg after 30 days30dExpires header expiredClient sends new request to server
4Server responds with new Expires header30dExpires: current_time + 30 daysClient caches updated resource again
💡 Client stops using cached resource after expiration time passes, triggering a new request
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Expires HeaderNonecurrent_time + 30 dayscurrent_time + 30 days (still valid)Expiredcurrent_time + 30 days (new)
Client Cache StateEmptyCached resource validCached resource validCache expiredCached resource valid
Key Moments - 2 Insights
Why does the client not request the resource again before the expiration time?
Because the Expires header tells the client the resource is still fresh, so it uses the cached copy without asking the server again, as shown in execution_table row 2.
What happens when the expiration time passes?
The client considers the cached resource stale and sends a new request to the server, as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Expires header value sent at step 1?
AExpires: current_time + 30 days
BExpires: current_time + 1 day
CExpires: current_time + 1 hour
DNo Expires header sent
💡 Hint
Check the 'Expires Header Sent' column in execution_table row 1
At which step does the client send a new request because the cached resource expired?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Effect on Client' column in execution_table row 3
If the expires directive was changed to '10d', how would the client's cache state change after step 2?
ACache would never expire
BCache would expire after 30 days
CCache would expire after 10 days instead of 30
DCache would expire immediately
💡 Hint
Refer to variable_tracker for 'Client Cache State' and how expiration time affects caching
Concept Snapshot
Expires directive in nginx sets how long browsers cache resources.
Syntax: expires <time>;
Example: expires 30d; caches for 30 days.
Nginx adds Expires header with future date.
Browsers use this to avoid re-requesting until expired.
Helps improve site speed by reducing server load.
Full Transcript
The Expires directive in nginx tells browsers how long to keep a resource cached. When a client requests a resource, nginx adds an Expires header with a date in the future based on the directive value. The client then caches the resource and uses it without asking the server again until the expiration date passes. After expiration, the client requests the resource again, and nginx sends a new Expires header to refresh the cache. This process helps reduce server load and speeds up page loading by avoiding unnecessary requests.