Cache-Control headers in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes for nginx to process Cache-Control headers changes as the number of requests grows.
Specifically, we ask: How does nginx handle many requests with Cache-Control settings efficiently?
Analyze the time complexity of the following nginx configuration snippet.
server {
location /static/ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}
}
This snippet sets Cache-Control headers for static files to tell browsers to cache them for 30 days.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks each incoming request to see if it matches the /static/ location and applies the Cache-Control header.
- How many times: This check happens once per request, no loops inside the config for each request.
As the number of requests increases, nginx performs the header check and addition for each request independently.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 header checks and additions |
| 100 | 100 header checks and additions |
| 1000 | 1000 header checks and additions |
Pattern observation: The work grows directly with the number of requests, one operation per request.
Time Complexity: O(n)
This means the time to process Cache-Control headers grows linearly with the number of requests.
[X] Wrong: "Adding Cache-Control headers slows down nginx exponentially as requests increase."
[OK] Correct: Each request is handled independently with a simple check, so the time grows linearly, not exponentially.
Understanding how nginx handles headers per request helps you explain server efficiency clearly and confidently in real-world situations.
"What if we added multiple Cache-Control rules for different locations? How would the time complexity change?"
Practice
Cache-Control header in nginx?Solution
Step 1: Understand HTTP headers role
HTTP headers communicate instructions between server and browser.Step 2: Identify Cache-Control header function
Cache-Control tells browsers how and when to cache content.Final Answer:
To tell browsers how to cache files -> Option CQuick Check:
Cache-Control = caching instructions [OK]
- Confusing Cache-Control with server IP settings
- Thinking Cache-Control manages SSL
- Mixing Cache-Control with hostname configuration
Solution
Step 1: Recall nginx syntax for headers
nginx usesadd_headerdirective to add HTTP headers.Step 2: Match correct syntax for Cache-Control
The correct syntax isadd_header Cache-Control "max-age=3600";to set caching for 3600 seconds (1 hour).Final Answer:
add_header Cache-Control "max-age=3600"; -> Option AQuick Check:
add_header + Cache-Control + max-age=3600 = correct [OK]
- Using incorrect directive names like set_header
- Wrong order of words in directive
- Missing quotes around header value
location /images/ {
add_header Cache-Control "public, max-age=86400";
}
What will the Cache-Control header instruct browsers for requests to /images/logo.png?Solution
Step 1: Analyze Cache-Control directives
"public" means cache is allowed by browsers and shared caches. "max-age=86400" means cache for 86400 seconds (1 day).Step 2: Apply to /images/logo.png request
Requests to /images/ get this header, so browsers and proxies cache the image for 1 day.Final Answer:
Cache the image for 1 day and allow shared caches -> Option BQuick Check:
public + max-age=86400 = 1 day shared cache [OK]
- Thinking "public" disables caching
- Confusing max-age seconds with hours
- Assuming private caching only
add_header Cache-Control "max-age=3600";But browsers still cache content longer than 1 hour. What is the likely problem?
Solution
Step 1: Understand add_header behavior with response codes
By default, nginx does not add headers with add_header on 304 Not Modified responses.Step 2: Identify why caching is longer
If response is 304, Cache-Control header may be missing, causing browsers to use old cache rules.Final Answer:
The add_header directive is inside a location block but response code is 304 -> Option DQuick Check:
add_header skips 304 responses by default [OK]
- Assuming max-age value controls server cache
- Thinking browsers ignore Cache-Control
- Believing nginx restart fixes header issues
Solution
Step 1: Prevent caching for API JSON responses
Using "no-store, no-cache, must-revalidate" ensures browsers do not cache API responses.Step 2: Allow caching for CSS files for 7 days
"public, max-age=604800" allows shared caches and browsers to cache CSS for 604800 seconds (7 days).Final Answer:
location /api/ { add_header Cache-Control "no-store, no-cache, must-revalidate"; } location /css/ { add_header Cache-Control "public, max-age=604800"; } -> Option AQuick Check:
no-cache API + public 7-day CSS = correct config [OK]
- Mixing cache directives for API and static files
- Using private instead of public for CSS caching
- Setting max-age=0 for static files incorrectly
