HSTS header in Nginx - Time & Space Complexity
We want to understand how adding the HSTS header affects nginx's work as requests come in.
Specifically, how does the server's effort change as more requests arrive?
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 443 ssl;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# other config...
}
This snippet adds the HSTS header to every HTTPS response to tell browsers to use HTTPS only.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding the HSTS header to each HTTPS response.
- How many times: Once per HTTPS request received by the server.
Each new HTTPS request causes nginx to add the HSTS header once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 header additions |
| 100 | 100 header additions |
| 1000 | 1000 header additions |
Pattern observation: The work grows directly with the number of requests.
Time Complexity: O(n)
This means the server's work to add the HSTS header grows linearly with the number of HTTPS requests.
[X] Wrong: "Adding the HSTS header slows down the server exponentially as requests increase."
[OK] Correct: Adding a header is a simple step done once per request, so the work grows steadily, not exponentially.
Understanding how simple configuration changes affect server work helps you explain performance impacts clearly and confidently.
"What if we added multiple headers instead of just one? How would the time complexity change?"
Practice
Strict-Transport-Security (HSTS) header in nginx?Solution
Step 1: Understand HSTS header purpose
The HSTS header tells browsers to only use HTTPS for the site, improving security.Step 2: Compare options with HSTS function
Only To force browsers to use HTTPS instead of HTTP for the site matches this purpose; others describe unrelated features.Final Answer:
To force browsers to use HTTPS instead of HTTP for the site -> Option AQuick Check:
HSTS = Force HTTPS [OK]
- Confusing HSTS with redirects
- Thinking HSTS controls caching
- Assuming HSTS compresses data
Solution
Step 1: Check correct syntax for add_header
The header value must be a quoted string with directives separated by semicolons, and 'always;' ensures it is added on all responses.Step 2: Validate option formatting
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; correctly quotes the value and uses semicolon between directives with 'always;'. Others miss quotes or semicolons or misplace 'always'.Final Answer:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; -> Option BQuick Check:
Quotes + semicolon + always = add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; [OK]
- Missing quotes around header value
- Forgetting semicolon between directives
- Placing 'always' inside quotes
add_header Strict-Transport-Security "max-age=86400; includeSubDomains" always;
What is the effect on browsers visiting the site?
Solution
Step 1: Interpret max-age and includeSubDomains
max-age=86400 means 86400 seconds = 1 day. includeSubDomains applies to all subdomains.Step 2: Understand browser behavior
Browsers will enforce HTTPS for 1 day on the site and all its subdomains as per header.Final Answer:
Browsers will enforce HTTPS for 1 day on the site and all subdomains -> Option AQuick Check:
86400 seconds = 1 day + includeSubDomains = Browsers will enforce HTTPS for 1 day on the site and all subdomains [OK]
- Confusing seconds with days
- Ignoring includeSubDomains effect
- Assuming max-age too low disables HSTS
add_header Strict-Transport-Security max-age=31536000 includeSubDomains always;
Why is the HSTS header not sent to clients?
Solution
Step 1: Check syntax for add_header directive
The header value must be enclosed in quotes. Missing quotes cause nginx to ignore the header.Step 2: Validate other parts
'always' must be outside quotes, max-age=31536000 is valid, includeSubDomains is valid directive.Final Answer:
The header value is missing quotes, causing syntax error -> Option DQuick Check:
Quotes missing = header not sent [OK]
- Placing 'always' inside quotes
- Not quoting header value
- Misunderstanding valid directives
Solution
Step 1: Calculate max-age for 6 months
6 months ≈ 15768000 seconds (6 * 30 * 24 * 3600).Step 2: Include required directives for preload
Preload requires max-age, includeSubDomains, and preload directive all present.Step 3: Verify syntax correctness
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always; correctly quotes the value, uses semicolons, includes all directives, and uses 'always;'.Final Answer:
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always; -> Option CQuick Check:
Preload + includeSubDomains + 6 months = add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always; [OK]
- Using wrong max-age for 6 months
- Omitting preload directive
- Not quoting header value
Ready to prove it?
Pass the test and this pattern moves unknown
