Bird
Raised Fist0
Nginxdevops~20 mins

Rewrite directive in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rewrite Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output status code after applying this rewrite?
Given the following nginx rewrite directive inside a server block:
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;

What HTTP status code will the client receive when requesting /oldpath/page1?
Nginx
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
A301 Moved Permanently
B302 Found
C404 Not Found
D500 Internal Server Error
Attempts:
2 left
💡 Hint
The keyword 'permanent' in rewrite directive sets a specific HTTP status code.
❓ Configuration
intermediate
2:00remaining
Which rewrite rule correctly redirects /blog to /news preserving the rest of the path?
You want to redirect all requests starting with /blog to /news, keeping the rest of the URL path intact. Which rewrite directive achieves this?
Arewrite ^/blog$ /news permanent;
Brewrite /blog /news permanent;
Crewrite ^/blog/(.*)$ /news/$1 permanent;
Drewrite ^/blog/(.*)$ /news permanent;
Attempts:
2 left
💡 Hint
Use a regex to capture the rest of the path and reuse it in the target.
❓ Troubleshoot
advanced
2:00remaining
Why does this rewrite not redirect as expected?
Given this nginx config snippet:
location /test/ {
  rewrite ^/test/(.*)$ /prod/$1;
}

Requests to /test/page do not redirect to /prod/page. Why?
AMissing redirect flag causes internal rewrite, not client redirect
BRegex pattern is incorrect and does not match the URL
CRewrite directive must be outside location blocks
DThe target URL must be absolute including domain
Attempts:
2 left
💡 Hint
Rewrite without flags performs internal rewrite, not external redirect.
🔀 Workflow
advanced
2:00remaining
Order the steps to apply a rewrite rule and test it successfully
Arrange these steps in the correct order to add and verify a rewrite rule in nginx:
A2,1,3,4
B1,2,3,4
C1,3,2,4
D3,1,2,4
Attempts:
2 left
💡 Hint
Always test config before reload, then verify with a request.
✅ Best Practice
expert
2:00remaining
Which rewrite directive is best to permanently redirect and avoid redirect loops?
You want to permanently redirect /old to /new only if the request is exactly /old. Which rewrite directive is best to avoid redirect loops and unnecessary redirects?
Arewrite /old /new permanent;
Brewrite ^/old/(.*)$ /new/$1 permanent;
Crewrite ^/old /new permanent;
Drewrite ^/old$ /new permanent;
Attempts:
2 left
💡 Hint
Match exact path to prevent redirect loops.

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