Bird
Raised Fist0
Nginxdevops~10 mins

Rewrite directive in Nginx - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the rewrite directive in nginx?
easy
A. To limit the number of client connections
B. To change URLs based on patterns for redirection or rewriting
C. To configure SSL certificates for secure connections
D. To serve static files directly from the server

Solution

  1. Step 1: Understand the role of rewrite directive

    The rewrite directive is used to modify URLs based on matching patterns.
  2. Step 2: Identify its main use

    It helps redirect or rewrite URLs without changing the visible link to the user.
  3. Final Answer:

    To change URLs based on patterns for redirection or rewriting -> Option B
  4. Quick Check:

    Rewrite directive = URL modification [OK]
Hint: Rewrite changes URLs by pattern matching [OK]
Common Mistakes:
  • Confusing rewrite with serving static files
  • Thinking rewrite manages SSL or security
  • Assuming rewrite limits connections
2. Which of the following is the correct syntax for a rewrite directive in nginx?
easy
A. rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
B. rewrite /oldpath/(.*) /newpath/$1 redirect
C. rewrite ^/oldpath/(.*)$ /newpath/$1 301
D. rewrite /oldpath/(.*)$ /newpath/$1 permanent

Solution

  1. Step 1: Check the rewrite syntax format

    The correct syntax is: rewrite <pattern> <replacement> <flag>;
  2. 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.
  3. Final Answer:

    rewrite ^/oldpath/(.*)$ /newpath/$1 permanent; -> Option A
  4. Quick Check:

    Correct syntax = rewrite + pattern + replacement + flag + semicolon [OK]
Hint: Rewrite ends with semicolon and uses flags like permanent [OK]
Common Mistakes:
  • Omitting the semicolon at the end
  • Using numeric flags instead of keywords
  • Missing caret (^) in regex pattern
3. Given this nginx config snippet:
rewrite ^/blog/(.*)$ /newblog/$1 last;

What happens when a user visits /blog/article1?
medium
A. The user is redirected to /newblog/article1 with a 301 status
B. The URL changes to /newblog/article1 and nginx restarts processing
C. The URL stays /blog/article1 but content from /newblog/article1 is served
D. An error occurs because 'last' is an invalid flag

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The URL stays /blog/article1 but content from /newblog/article1 is served -> Option C
  4. Quick Check:

    'last' flag = internal rewrite, URL unchanged [OK]
Hint: 'last' flag restarts processing with new URL [OK]
Common Mistakes:
  • Confusing 'last' with external redirect
  • Thinking URL visible to user changes
  • Assuming 'last' flag causes error
4. Identify the error in this nginx rewrite directive:
rewrite ^/old/(.*)$ /new/$1 permanent
medium
A. Missing semicolon at the end of the directive
B. Incorrect regex pattern syntax
C. Invalid flag 'permanent' used
D. Rewrite directive cannot use variables like $1

Solution

  1. Step 1: Check syntax completeness

    Every nginx directive must end with a semicolon (;). This line is missing it.
  2. Step 2: Validate other parts

    The regex pattern and flag 'permanent' are correct, and $1 is valid for capturing groups.
  3. Final Answer:

    Missing semicolon at the end of the directive -> Option A
  4. Quick Check:

    Missing semicolon = syntax error [OK]
Hint: Always end rewrite lines with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolon at line end
  • Misunderstanding regex syntax
  • Thinking $1 is invalid in rewrite
5. You want to redirect all requests from /shop/ to /store/ permanently, but only if the URL ends exactly with /shop/ (no extra path after). Which rewrite directive achieves this?
hard
A. rewrite ^/shop/(.*)$ /store/$1 permanent;
B. rewrite ^/shop$ /store/ permanent;
C. rewrite /shop/ /store/ permanent;
D. rewrite ^/shop/$ /store/ permanent;

Solution

  1. Step 1: Match only exact /shop/ URL

    The regex ^/shop/$ matches exactly '/shop/' with nothing after it.
  2. Step 2: Redirect permanently to /store/

    The replacement is /store/ with the 'permanent' flag for 301 redirect.
  3. Final Answer:

    rewrite ^/shop/$ /store/ permanent; -> Option D
  4. Quick Check:

    Exact match with ^/shop/$ and permanent redirect [OK]
Hint: Use ^ and $ to match exact URL in rewrite [OK]
Common Mistakes:
  • Using (.*) which matches extra paths
  • Omitting anchors ^ and $
  • Incorrect regex without end anchor
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard