Rewrite directive in Nginx - Time & Space Complexity
We want to understand how the time it takes for nginx to process rewrite rules changes as we add more rules.
Specifically, how does the number of rewrite directives affect the work nginx does?
Analyze the time complexity of the following nginx rewrite rules.
server {
listen 80;
server_name example.com;
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
rewrite ^/blog/(.*)$ /news/$1 last;
rewrite ^/user/(.*)$ /profile/$1 break;
}
This snippet shows three rewrite directives that change URLs based on patterns.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks each rewrite rule one by one to see if the URL matches the pattern.
- How many times: It tries each rewrite directive in order until one matches or all are checked.
As the number of rewrite rules grows, nginx has to check more patterns for each request.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 pattern checks |
| 10 | 10 pattern checks |
| 100 | 100 pattern checks |
Pattern observation: The number of checks grows directly with the number of rewrite rules.
Time Complexity: O(n)
This means the time to process rewrites grows linearly with the number of rewrite directives.
[X] Wrong: "Adding more rewrite rules does not affect performance because nginx is very fast."
[OK] Correct: Even though nginx is fast, each rewrite rule adds work because nginx checks them one by one, so more rules mean more checks and more time.
Understanding how rewrite rules affect processing time helps you design efficient server configurations and shows you can think about performance in real setups.
What if we combined multiple rewrite rules into one using a more complex pattern? How would the time complexity change?
Practice
rewrite directive in nginx?Solution
Step 1: Understand the role of rewrite directive
The rewrite directive is used to modify URLs based on matching patterns.Step 2: Identify its main use
It helps redirect or rewrite URLs without changing the visible link to the user.Final Answer:
To change URLs based on patterns for redirection or rewriting -> Option BQuick Check:
Rewrite directive = URL modification [OK]
- Confusing rewrite with serving static files
- Thinking rewrite manages SSL or security
- Assuming rewrite limits connections
Solution
Step 1: Check the rewrite syntax format
The correct syntax is: rewrite <pattern> <replacement> <flag>;Step 2: Identify the correct flag and semicolon usage
The flag must be a recognized keyword like 'permanent' and the line must end with a semicolon.Final Answer:
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent; -> Option AQuick Check:
Correct syntax = rewrite + pattern + replacement + flag + semicolon [OK]
- Omitting the semicolon at the end
- Using numeric flags instead of keywords
- Missing caret (^) in regex pattern
rewrite ^/blog/(.*)$ /newblog/$1 last;
What happens when a user visits
/blog/article1?Solution
Step 1: Understand the 'last' flag in rewrite
The 'last' flag performs an internal rewrite: nginx changes the URI internally and restarts location processing with the new URI. The browser URL remains unchanged.Step 2: Apply to the example URL
Visiting /blog/article1 matches the pattern, rewrites internally to /newblog/article1, and serves content from there without changing the visible URL.Final Answer:
The URL stays /blog/article1 but content from /newblog/article1 is served -> Option CQuick Check:
'last' flag = internal rewrite, URL unchanged [OK]
- Confusing 'last' with external redirect
- Thinking URL visible to user changes
- Assuming 'last' flag causes error
rewrite ^/old/(.*)$ /new/$1 permanent
Solution
Step 1: Check syntax completeness
Every nginx directive must end with a semicolon (;). This line is missing it.Step 2: Validate other parts
The regex pattern and flag 'permanent' are correct, and $1 is valid for capturing groups.Final Answer:
Missing semicolon at the end of the directive -> Option AQuick Check:
Missing semicolon = syntax error [OK]
- Forgetting semicolon at line end
- Misunderstanding regex syntax
- Thinking $1 is invalid in rewrite
/shop/ to /store/ permanently, but only if the URL ends exactly with /shop/ (no extra path after). Which rewrite directive achieves this?Solution
Step 1: Match only exact /shop/ URL
The regex ^/shop/$ matches exactly '/shop/' with nothing after it.Step 2: Redirect permanently to /store/
The replacement is /store/ with the 'permanent' flag for 301 redirect.Final Answer:
rewrite ^/shop/$ /store/ permanent; -> Option DQuick Check:
Exact match with ^/shop/$ and permanent redirect [OK]
- Using (.*) which matches extra paths
- Omitting anchors ^ and $
- Incorrect regex without end anchor
Ready to prove it?
Pass the test and this pattern moves unknown
