Expires directive in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the Expires directive affects nginx's work as it handles many requests.
Specifically, how does the time nginx spends change when more files have expiration times set?
Analyze the time complexity of the following nginx configuration snippet.
location /images/ {
expires 30d;
add_header Cache-Control "public";
}
This snippet sets a 30-day expiration for files in the /images/ folder, telling browsers to cache them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks the request URL against the location and applies the expires rule.
- How many times: This check happens once per request, no loops over multiple files.
Each request triggers a simple check and header addition, regardless of how many files have expires set.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 requests | 10 checks and header additions |
| 100 requests | 100 checks and header additions |
| 1000 requests | 1000 checks and header additions |
Pattern observation: The work grows directly with the number of requests, not with the number of files configured.
Time Complexity: O(n)
This means nginx spends time proportional to the number of requests, doing a quick check each time.
[X] Wrong: "Setting expires on many files makes nginx slower for each request."
[OK] Correct: nginx applies expires per request without scanning all files, so the number of files with expires does not slow down each request.
Understanding how configuration directives affect request handling time helps you explain performance impacts clearly and confidently.
"What if we added multiple expires directives in nested locations? How would that affect the time complexity per request?"
Practice
expires directive in an nginx configuration?Solution
Step 1: Understand the role of
Theexpiresin nginxexpiresdirective 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 BQuick Check:
Expires directive = browser cache time [OK]
- Confusing expires with server timezone settings
- Thinking expires limits connections
- Mixing expires with upload size limits
expires directive to 1 day in nginx?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 DQuick Check:
Correct syntax ends with semicolon and uses short time unit [OK]
- Omitting semicolon at the end
- Using spaces in time value
- Writing full words like 'day' instead of 'd'
location ~* \.(jpg|jpeg|png)$ {
expires 30d;
}What will the browser do when accessing a PNG file?
Solution
Step 1: Analyze the regex and expires directive
The location matches .jpg, .jpeg, and .png files and setsexpires 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 CQuick Check:
Expires 30d means 30 days cache [OK]
- Confusing days with seconds
- Ignoring regex file matching
- Assuming no caching without explicit 'no-cache'
location /static/ {
expires 10days;
}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 AQuick Check:
Use short time units like 'd' not full words [OK]
- Writing full words for time units
- Forgetting semicolon (not the case here)
- Thinking expires can't be in location block
Solution
Step 1: Recall how to disable caching in nginx
Settingexpires -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; } usesexpires 7d;for CSS andexpires -1;for HTML, which is correct. Others use invalid or incorrect values.Final Answer:
location ~* \.css$ { expires 7d; } location ~* \.html$ { expires -1; } -> Option AQuick Check:
Use 'expires -1;' to disable caching [OK]
- Using 'expires off;' which is invalid
- Using 'expires 0;' which sets immediate expiry but not no-cache
- Using 'expires never;' which is not valid syntax
