Request/response transformation in Nginx - Time & Space Complexity
When nginx changes requests or responses, it does some work for each request it handles.
We want to know how this work grows as more requests come in or as the data changes.
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
sub_filter 'foo' 'bar';
sub_filter_once off;
}
}
This snippet forwards requests to a backend and replaces all occurrences of 'foo' with 'bar' in the response.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the entire response body to find and replace all 'foo' strings.
- How many times: Once per response, for every byte in the response body.
The time to transform the response grows as the response size grows because nginx checks each part of the response.
| Input Size (n) - response bytes | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the size of the response.
Time Complexity: O(n)
This means the time to transform the response grows linearly with the response size.
[X] Wrong: "The transformation time stays the same no matter how big the response is."
[OK] Correct: Because nginx must check every part of the response to find all matches, bigger responses take more time.
Understanding how request and response transformations scale helps you explain performance impacts clearly and shows you think about real-world server behavior.
"What if nginx only replaced the first occurrence of 'foo' instead of all? How would the time complexity change?"
Practice
Solution
Step 1: Understand request/response transformation
This process changes data in requests or responses to improve how clients and servers communicate.Step 2: Identify the main goal in nginx context
nginx uses this to modify headers, URLs, or content to suit needs, not hardware or blocking.Final Answer:
To modify data passing through nginx for better communication -> Option DQuick Check:
Transformation means changing data flow [OK]
- Confusing transformation with hardware upgrades
- Thinking it blocks requests instead of modifying them
- Assuming it changes server IP automatically
Solution
Step 1: Identify directives for header modification
proxy_set_header changes headers sent to the backend server.Step 2: Differentiate from other directives
add_header adds headers to responses, rewrite changes URLs, gzip_disable disables compression.Final Answer:
proxy_set_header -> Option BQuick Check:
Request header modification = proxy_set_header [OK]
- Using add_header which affects response headers
- Confusing rewrite with header modification
- Trying gzip_disable for headers
X-Transformed value?location / {
proxy_pass http://backend;
proxy_set_header X-Original $http_x_original;
add_header X-Transformed "Yes";
}Solution
Step 1: Understand add_header directive effect
add_header adds or modifies response headers sent to the client; here it sets X-Transformed to "Yes".Step 2: Differentiate proxy_set_header role
proxy_set_header modifies request headers sent to backend, not response headers.Final Answer:
Yes -> Option AQuick Check:
add_header sets response header = Yes [OK]
- Confusing request and response headers
- Expecting variable names as header values
- Ignoring add_header effect
Solution
Step 1: Understand response body modification requirements
Modifying response body requires disabling gzip compression because compressed data can't be changed easily.Step 2: Identify common misconfigurations
If gzip is enabled, changes won't show; proxy_pass or listen missing causes other errors, add_header affects headers only.Final Answer:
gzip compression is enabled, preventing body modification -> Option AQuick Check:
Disable gzip to modify response body [OK]
- Ignoring gzip compression effect
- Confusing header directives with body changes
- Assuming missing proxy_pass causes this issue
/old-path/ to /new-path/ and add a response header X-Updated: true. Which nginx config snippet correctly does both?Solution
Step 1: Choose correct location and rewrite usage
Rewrite must be inside location matching /old-path/ to rewrite URLs internally with 'break' to stop processing.Step 2: Add response header and proxy_pass correctly
add_header adds header to response; proxy_pass forwards request to backend after rewrite.Step 3: Identify incorrect options
The configlocation /old-path/ { proxy_pass http://backend; add_header X-Updated true; rewrite ^/old-path/(.*)$ /new-path/$1 break; }places rewrite after proxy_pass (ignored). Configs usinglocation /new-path/have the wrong location. Configs withpermanentcause redirects, not internal rewrites.Final Answer:
location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; } -> Option CQuick Check:
Internal rewrite + header + proxy_pass = location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; } [OK]
- Using permanent instead of break for internal rewrite
- Placing rewrite after proxy_pass
- Using wrong location block for rewrite
Ready to prove it?
Pass the test and this pattern moves unknown
