0
0
Nginxdevops~10 mins

Rewrite directive in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Rewrite directive
Request comes in
Check rewrite rule pattern
Rewrite URL
Redirect or internal rewrite
Send response
The rewrite directive checks if the incoming URL matches a pattern. If yes, it rewrites the URL and either redirects or internally rewrites before sending the response.
Execution Sample
Nginx
server {
  listen 80;
  rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
}
This config rewrites URLs starting with /oldpath/ to /newpath/ with a permanent redirect.
Process Table
StepIncoming URLRewrite PatternMatch ResultActionResulting URL
1/oldpath/page1^/oldpath/(.*)$MatchRewrite with permanent redirect/newpath/page1
2/oldpath/page2^/oldpath/(.*)$MatchRewrite with permanent redirect/newpath/page2
3/otherpath/page^/oldpath/(.*)$No MatchServe original URL/otherpath/page
💡 Execution stops after rewrite or serving original URL based on match.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3
Incoming URL/oldpath/page1/oldpath/page2/otherpath/page/otherpath/page
Resulting URLN/A/newpath/page1/newpath/page2/otherpath/page
Key Moments - 2 Insights
Why does the URL change only when the pattern matches?
Because the rewrite directive only applies its action if the incoming URL matches the specified pattern, as shown in execution_table rows 1 and 3.
What does 'permanent' mean in the rewrite directive?
'Permanent' means the server sends a 301 redirect telling the browser the URL has moved permanently, causing the browser to update the URL, as seen in the action column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the resulting URL at step 2?
A/newpath/page2
B/oldpath/page2
C/otherpath/page
D/newpath/page1
💡 Hint
Check the 'Resulting URL' column in row 2 of the execution_table.
At which step does the rewrite pattern NOT match the incoming URL?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at the 'Match Result' column in the execution_table.
If we remove 'permanent' from the rewrite directive, what changes in the execution?
AThe URL is rewritten internally without redirecting the browser
BThe URL will not change at all
CThe server will return an error
DThe rewrite pattern will not match
💡 Hint
Consider the meaning of 'permanent' in the action column of the execution_table.
Concept Snapshot
rewrite directive syntax:
rewrite regex replacement [flag];

Checks if URL matches regex.
If yes, rewrites URL to replacement.
Flags control redirect type (e.g., permanent = 301).
Used for URL changes or redirects.
Full Transcript
The nginx rewrite directive checks incoming URLs against a pattern. If the URL matches, it rewrites the URL to a new one. This can be an internal rewrite or a redirect. For example, URLs starting with /oldpath/ can be redirected permanently to /newpath/. If the URL does not match, it is served as is. The 'permanent' flag sends a 301 redirect to the browser. This process helps manage URL changes smoothly.