Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the expires directive in nginx?
The expires directive sets the caching time for responses, telling browsers how long to keep files before requesting them again.
Click to reveal answer
beginner
How do you set a file to expire 1 hour after it is served using the expires directive?
Use expires 1h; inside the location or server block to tell browsers to cache the file for 1 hour.
Click to reveal answer
intermediate
What does expires max; do in nginx?
It sets the cache expiration to a very long time (about 10 years), effectively telling browsers to keep the file indefinitely.
Click to reveal answer
intermediate
How does the expires directive affect HTTP headers?
It modifies the Expires and Cache-Control headers to control browser caching behavior.
Click to reveal answer
advanced
Can the expires directive use negative values? What happens if you use expires -1;?
Yes, negative values are allowed. expires -1; tells browsers not to cache the response and to revalidate it every time.
Click to reveal answer
What does expires 30d; mean in an nginx config?
AExpire the file immediately
BCache the file for 30 days
CCache the file for 30 minutes
DDisable caching
✗ Incorrect
expires 30d; tells browsers to cache the file for 30 days before requesting it again.
Which HTTP headers does the expires directive modify?
AUser-Agent and Referer
BContent-Type and Content-Length
CAuthorization and Cookie
DExpires and Cache-Control
✗ Incorrect
The expires directive sets the Expires and Cache-Control headers to control caching.
What happens if you set expires off; in nginx?
ACaches files forever
BSets cache to 1 hour
CDisables setting the Expires header
DCauses an error
✗ Incorrect
expires off; disables adding the Expires header, so caching is not controlled by nginx.
How would you tell nginx to set caching to 10 minutes?
Aexpires 10m;
Bexpires 10h;
Cexpires 10s;
Dexpires 10d;
✗ Incorrect
expires 10m; sets the cache time to 10 minutes.
What is the effect of expires -1; in nginx?
APrevents caching and forces revalidation
BCaches for 1 second
CCaches forever
DCaches for 1 minute
✗ Incorrect
expires -1; tells browsers not to cache and to check with the server every time.
Explain how the expires directive controls browser caching in nginx.
Think about how browsers know when to ask for a new file.
You got /5 concepts.
Describe a scenario where you would use expires max; and why.
Consider files that don’t change often and can be cached for a long time.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the expires directive in an nginx configuration?
easy
A. To specify the file upload size limit
B. To control how long browsers cache files before requesting them again
C. To limit the number of simultaneous connections
D. To set the server's time zone
Solution
Step 1: Understand the role of expires in nginx
The expires directive tells browsers how long to keep files cached before checking for updates.
Step 2: Compare options with this function
Only To control how long browsers cache files before requesting them again matches this purpose; others relate to different server settings.
Final Answer:
To control how long browsers cache files before requesting them again -> Option B
Quick Check:
Expires directive = browser cache time [OK]
Hint: Expires controls browser cache duration for files [OK]
Common Mistakes:
Confusing expires with server timezone settings
Thinking expires limits connections
Mixing expires with upload size limits
2. Which of the following is the correct syntax to set the expires directive to 1 day in nginx?
easy
A. expires = 1 day;
B. expires 1 day
C. expires 24hours;
D. expires 1d;
Solution
Step 1: Recall nginx expires syntax
The correct syntax uses a time value followed by a semicolon, e.g., expires 1d; for one day.
Step 2: Check each option for syntax correctness
expires = 1 day; uses invalid '=' and full word 'day'; expires 1 day lacks semicolon; expires 24hours; uses invalid time unit '24hours'. Only B matches correct syntax: expires 1d;.
Final Answer:
expires 1d; -> Option D
Quick Check:
Correct syntax ends with semicolon and uses short time unit [OK]
Hint: Use short time units with semicolon, like 'expires 1d;' [OK]
Common Mistakes:
Omitting semicolon at the end
Using spaces in time value
Writing full words like 'day' instead of 'd'
3. Given this nginx config snippet:
location ~* \.(jpg|jpeg|png)$ {
expires 30d;
}
What will the browser do when accessing a PNG file?
medium
A. Cache the PNG file for 30 seconds
B. Never cache the PNG file
C. Cache the PNG file for 30 days before re-requesting
D. Immediately re-request the PNG file every time
Solution
Step 1: Analyze the regex and expires directive
The location matches .jpg, .jpeg, and .png files and sets expires 30d;, meaning 30 days caching.
Step 2: Understand browser caching behavior
Browsers will keep the PNG file cached for 30 days before checking for updates.
Final Answer:
Cache the PNG file for 30 days before re-requesting -> Option C
Quick Check:
Expires 30d means 30 days cache [OK]
Hint: Regex matches file types; expires sets cache time [OK]
Common Mistakes:
Confusing days with seconds
Ignoring regex file matching
Assuming no caching without explicit 'no-cache'
4. Identify the error in this nginx config:
location /static/ {
expires 10days;
}
medium
A. The time unit '10days' is invalid; should be '10d'
B. Missing semicolon after expires directive
C. Location block syntax is incorrect
D. Expires directive cannot be used inside location
Solution
Step 1: Check the expires time unit
The correct time unit uses short forms like 'd' for days. '10days' is invalid syntax.
Step 2: Verify other syntax elements
Semicolon is present, location syntax is correct, and expires can be used inside location.
Final Answer:
The time unit '10days' is invalid; should be '10d' -> Option A
Quick Check:
Use short time units like 'd' not full words [OK]
Hint: Use short units like 'd' for days, not full words [OK]
Common Mistakes:
Writing full words for time units
Forgetting semicolon (not the case here)
Thinking expires can't be in location block
5. You want to set caching so that CSS files are cached for 7 days, but HTML files are never cached. Which nginx config snippet achieves this?
Setting expires -1; disables caching (forces no cache) for files.
Step 2: Check each option for correct disables and enables
location ~* \.css$ { expires 7d; } location ~* \.html$ { expires -1; } uses expires 7d; for CSS and expires -1; for HTML, which is correct. Others use invalid or incorrect values.