HTTP to HTTPS redirect in Nginx - Time & Space Complexity
We want to understand how the time it takes to redirect HTTP requests to HTTPS changes as more requests come in.
Specifically, we ask: How does the server handle many requests efficiently when redirecting?
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
This code listens on HTTP port 80 and redirects all requests to the HTTPS version of the same URL.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming HTTP request and issuing a redirect.
- How many times: Once per request, independently for each request.
Each new HTTP request triggers one redirect operation. The time to handle each request stays about the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 redirect operations |
| 100 | 100 redirect operations |
| 1000 | 1000 redirect operations |
Pattern observation: The total work grows directly with the number of requests, but each request is handled quickly and separately.
Time Complexity: O(n)
This means the total time to handle all requests grows linearly with the number of requests.
[X] Wrong: "The redirect happens once and then all requests are automatically redirected without extra work."
[OK] Correct: Each request is separate and must be processed individually by the server to send the redirect response.
Understanding how servers handle many requests helps you explain real-world web traffic handling and scaling in interviews.
"What if we added caching for redirects? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand HTTP and HTTPS roles
HTTP is not secure, HTTPS encrypts data for safety.Step 2: Purpose of redirect
Redirecting HTTP to HTTPS ensures users always use the secure version.Final Answer:
To automatically send users from an insecure HTTP site to a secure HTTPS site -> Option CQuick Check:
Redirect HTTP to HTTPS = Secure site [OK]
- Thinking redirect blocks HTTP instead of forwarding
- Believing HTTPS disables HTTP completely
- Confusing redirect with allowing both protocols
Solution
Step 1: Identify correct listen port and redirect method
HTTP listens on port 80; redirect uses 'return 301' for permanent redirect.Step 2: Check syntax correctness
server { listen 80; return 301 https://$host$request_uri; } uses 'listen 80' and 'return 301 https://$host$request_uri;' which is correct syntax.Final Answer:
server { listen 80; return 301 https://$host$request_uri; } -> Option DQuick Check:
Port 80 + return 301 + https URL = Correct redirect [OK]
- Using 'listen 443' for HTTP redirect
- Using 'proxy_pass' instead of 'return 301'
- Incorrect rewrite syntax causing errors
http://example.com/page?
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}Solution
Step 1: Analyze the server block
It listens on port 80 (HTTP) and returns a 301 redirect to HTTPS version of the requested URI.Step 2: Understand the redirect effect
User visiting http://example.com/page will be sent a permanent redirect to https://example.com/page.Final Answer:
The user will be redirected to https://example.com/page with a permanent redirect -> Option BQuick Check:
HTTP request + return 301 to HTTPS = Redirect [OK]
- Assuming no redirect happens
- Confusing proxy_pass with redirect
- Expecting error instead of redirect
server {
listen 80;
server_name example.com;
rewrite ^ https://$host$uri permanent;
}
What is the likely problem?Solution
Step 1: Check rewrite vs return usage
While rewrite can redirect, using 'return 301' is simpler and more reliable for HTTP to HTTPS redirects.Step 2: Identify best practice
Replacing rewrite with 'return 301 https://$host$request_uri;' fixes the redirect issue.Final Answer:
The rewrite directive should be replaced with 'return 301 https://$host$request_uri;' -> Option AQuick Check:
Use 'return 301' for clean HTTP to HTTPS redirect [OK]
- Thinking missing semicolon causes failure
- Changing listen port to 443 for HTTP redirect
- Assuming server_name is the problem
secure.example.com, leaving other domains on HTTP. Which nginx config snippet achieves this?Solution
Step 1: Match only the desired domain
Using 'server_name secure.example.com;' limits redirect to that domain only.Step 2: Redirect HTTP on port 80 to HTTPS
Listening on port 80 and returning 301 redirect to HTTPS URL ensures only that domain redirects.Final Answer:
server { listen 80; server_name secure.example.com; return 301 https://$host$request_uri; } -> Option AQuick Check:
Specific server_name + return 301 = domain-only redirect [OK]
- Using default_server redirects all domains
- Redirecting on port 443 instead of 80
- Using wildcard server_name '_' incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
