Adding response headers (add_header) in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When nginx adds response headers, it processes each header line before sending the response.
We want to understand how the time to add headers grows as the number of headers increases.
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 80;
location / {
add_header X-Custom-1 "Value1";
add_header X-Custom-2 "Value2";
add_header X-Custom-3 "Value3";
# ... more add_header directives ...
}
}
This snippet adds multiple custom headers to each HTTP response served by nginx.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each header line to the response.
- How many times: Once per header configured with
add_header.
As the number of headers increases, nginx processes each header one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 headers | 10 operations |
| 100 headers | 100 operations |
| 1000 headers | 1000 operations |
Pattern observation: The work grows directly with the number of headers added.
Time Complexity: O(n)
This means the time to add headers grows linearly with the number of headers configured.
[X] Wrong: "Adding more headers does not affect response time because headers are small."
[OK] Correct: Even small headers require processing; more headers mean more work, so response time grows with header count.
Understanding how configuration directives like add_header scale helps you reason about server performance and response times in real projects.
What if nginx cached the headers once instead of adding them on every response? How would the time complexity change?
Practice
add_header directive in nginx?Solution
Step 1: Understand the role of
Theadd_headeradd_headerdirective is used to add extra HTTP headers to responses sent by nginx.Step 2: Compare with other options
Redirecting requests, configuring ports, and defining root directories are unrelated to adding headers.Final Answer:
To add extra information to HTTP responses -> Option BQuick Check:
add_headeradds headers [OK]
- Confusing add_header with redirect directives
- Thinking add_header sets server ports
- Assuming add_header changes root directory
X-Custom-Header with value MyValue in nginx?Solution
Step 1: Recall nginx add_header syntax
The correct syntax isadd_header name value;without extra symbols like = or =>.Step 2: Validate each option
add_header X-Custom-Header MyValue; matches correct syntax. Options A, C, and D use invalid syntax with quotes or symbols.Final Answer:
add_header X-Custom-Header MyValue; -> Option CQuick Check:
Syntax isadd_header name value;[OK]
- Adding quotes around header name and value
- Using = or => symbols incorrectly
- Missing semicolon at the end
add_header X-Test "Hello";
location /error {
return 404;
}What happens when a client requests
/error?Solution
Step 1: Understand default add_header behavior on errors
By default,add_headerdoes NOT add headers on error responses like 404.Step 2: Analyze the config and request
The location returns 404, soX-Testheader is omitted unlessalwaysis used.Final Answer:
The response returns 404 without X-Test header -> Option DQuick Check:
Headers not added on errors without always [OK]
- Assuming headers always appear on error responses
- Confusing return status with header presence
- Expecting 200 OK instead of 404
X-Frame-Options: DENY to all responses including errors. Which nginx config fixes this incorrect snippet?add_header X-Frame-Options DENY;
But headers are missing on 404 pages.
Solution
Step 1: Identify why headers are missing on errors
By default,add_headerskips error responses unlessalwaysis added.Step 2: Fix syntax to include headers on all responses
Addingalwayson the same line ensures headers appear even on errors.Final Answer:
Change to add_header X-Frame-Options DENY always; -> Option AQuick Check:
Use 'always' on same line to add headers on errors [OK]
- Placing 'always' on a separate line
- Using invalid keywords like 'on_error'
- Moving add_header inside unrelated blocks
Cache-Control: no-store for all responses, and Strict-Transport-Security: max-age=31536000 only for successful responses (status 200-299). Which nginx config achieves this correctly?Solution
Step 1: Understand 'always' effect on headers
Thealwaysflag makes headers appear on all responses including errors.Step 2: Apply 'always' only to Cache-Control
We wantCache-Controlon all responses, so addalwaysthere. ForStrict-Transport-Security, omitalwaysto restrict to 2xx responses.Final Answer:
add_header Cache-Control no-store always; add_header Strict-Transport-Security max-age=31536000; -> Option AQuick Check:
'always' for all responses, omit for success-only [OK]
- Adding 'always' to all headers causing unwanted error headers
- Omitting 'always' for headers needed on errors
- Misunderstanding which responses get headers without 'always'
