CORS configuration in Nginx - Time & Space Complexity
We want to understand how the time it takes to handle CORS settings in nginx changes as more requests come in.
Specifically, how does nginx process CORS headers for each request?
Analyze the time complexity of the following nginx CORS configuration snippet.
location /api/ {
if ($http_origin) {
add_header Access-Control-Allow-Origin "$http_origin";
add_header Access-Control-Allow-Credentials "true";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
add_header Access-Control-Allow-Headers "Authorization,Content-Type";
}
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_pass http://backend;
}
This code sets CORS headers for requests to /api/ and handles preflight OPTIONS requests.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx processes each incoming HTTP request and applies the CORS header rules.
- How many times: Once per request; no loops or recursion inside the configuration.
Each request triggers the same fixed set of header additions and checks.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sets of header checks and additions |
| 100 | 100 sets of header checks and additions |
| 1000 | 1000 sets of header checks and additions |
Pattern observation: The work grows linearly with the number of requests, but each request's processing time stays constant.
Time Complexity: O(n)
This means the total work grows directly with the number of requests, but each request is handled in constant time.
[X] Wrong: "Adding more CORS headers makes each request take longer in a way that grows with the number of headers."
[OK] Correct: The number of headers is fixed and small, so each request still takes about the same time regardless of header count.
Understanding how nginx handles CORS per request helps you explain server behavior clearly and shows you can reason about request handling efficiency.
"What if we added a loop inside nginx config to check multiple origins dynamically? How would the time complexity change?"
Practice
nginx server?Solution
Step 1: Understand CORS purpose
CORS (Cross-Origin Resource Sharing) controls which external websites can request resources from your server.Step 2: Identify correct purpose in options
Only To control which websites can access server resources describes controlling access from other websites, which matches CORS functionality.Final Answer:
To control which websites can access server resources -> Option AQuick Check:
CORS controls access = D [OK]
- Confusing CORS with encryption or caching
- Thinking CORS blocks all traffic
- Assuming CORS improves performance
nginx configuration?Solution
Step 1: Understand nginx add_header syntax
The header name does not require quotes unless it contains spaces or special characters. The value must be quoted if it is a string like "*".Step 2: Identify correct quoting
add_header Access-Control-Allow-Origin "*"; correctly quotes the value and does not quote the header name, which is the recommended syntax.Final Answer:
add_header Access-Control-Allow-Origin "*"; -> Option CQuick Check:
Proper quoting for header value only = B [OK]
- Quoting header name unnecessarily
- Using * without quotes
- Misplacing semicolons
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
add_header Access-Control-Max-Age 1728000;
add_header Content-Type "text/plain charset=UTF-8";
add_header Content-Length 0;
return 204;
}
What will be the HTTP status code returned for an OPTIONS request?Solution
Step 1: Analyze the if block for OPTIONS method
The configuration checks if the request method is OPTIONS, then sets headers and returns status code 204.Step 2: Identify the return status code
The linereturn 204;explicitly sends HTTP status 204 (No Content) for OPTIONS requests.Final Answer:
204 -> Option BQuick Check:
OPTIONS returns 204 No Content = B [OK]
- Assuming default 200 OK instead of 204
- Confusing OPTIONS with GET method
- Ignoring explicit return codes
add_header Access-Control-Allow-Origin *;But browsers still block cross-origin requests. What is the likely problem?
Solution
Step 1: Understand add_header behavior in nginx
By default, add_header only adds headers on successful (2xx) responses. For OPTIONS or error responses, headers may be missing unless 'always' is used.Step 2: Identify missing 'always' parameter
Without 'always', CORS headers may not be sent on OPTIONS or error responses, causing browser blocks.Final Answer:
The add_header directive is inside a location block without 'always' parameter -> Option AQuick Check:
Missing 'always' causes headers to be skipped = A [OK]
- Not using 'always' with add_header
- Assuming nginx lacks CORS support
- Forgetting to reload nginx after config changes
https://example.com and support preflight OPTIONS requests. Which configuration snippet correctly achieves this?Solution
Step 1: Check origin restriction
Only add_header "Access-Control-Allow-Origin" "https://example.com" always; if ($request_method = 'OPTIONS') { add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS" always; add_header "Access-Control-Allow-Headers" "Content-Type" always; add_header "Access-Control-Max-Age" 1728000 always; return 204; } restricts origin to "https://example.com" correctly with proper quoting and 'always' parameter.Step 2: Verify OPTIONS preflight handling
The configuration handles OPTIONS requests by returning 204 with required CORS headers and 'always' to ensure headers are sent.Step 3: Eliminate incorrect options
Snippets using '*' allow all origins instead of restricting to https://example.com; those lacking full OPTIONS handling, 'always' parameter, or returning 200 instead of 204 fail preflight checks.Final Answer:
Correctly restricts origin and handles OPTIONS with headers and 204 -> Option DQuick Check:
Restrict origin + OPTIONS 204 + always = C [OK]
- Using wildcard '*' instead of specific origin
- Not handling OPTIONS preflight properly
- Missing 'always' causing headers to be skipped
Ready to prove it?
Pass the test and this pattern moves unknown
