Bird
Raised Fist0
Nginxdevops~20 mins

Rewrite flags (last, break, redirect, permanent) in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rewrite Flags Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What happens when using the 'last' rewrite flag?
Given this nginx rewrite rule:
rewrite ^/oldpath/(.*)$ /newpath/$1 last;

What is the effect of the 'last' flag in this context?
Nginx
rewrite ^/oldpath/(.*)$ /newpath/$1 last;
ASends a 301 permanent redirect to the client with the rewritten URI.
BStops processing current rewrite directives and restarts location search with the new URI.
CStops processing rewrite directives and serves the rewritten URI immediately without further processing.
DSends a 302 temporary redirect to the client with the rewritten URI.
Attempts:
2 left
💡 Hint
Think about how nginx handles URI rewriting and location matching after the rewrite.
💻 Command Output
intermediate
2:00remaining
What does the 'break' rewrite flag do in nginx?
Consider this rewrite rule:
rewrite ^/temp/(.*)$ /final/$1 break;

What is the behavior of the 'break' flag here?
Nginx
rewrite ^/temp/(.*)$ /final/$1 break;
ASends a 301 redirect to the client with the rewritten URI.
BRestarts location search with the rewritten URI.
CSends a 302 redirect to the client with the rewritten URI.
DStops processing rewrite directives and continues processing the request within the current location using the rewritten URI.
Attempts:
2 left
💡 Hint
Think about whether nginx changes location blocks after this rewrite.
💻 Command Output
advanced
2:00remaining
What is the HTTP response when using the 'redirect' rewrite flag?
Given this rewrite rule:
rewrite ^/old/(.*)$ /new/$1 redirect;

What HTTP status code does nginx send to the client?
Nginx
rewrite ^/old/(.*)$ /new/$1 redirect;
A302 Found (Temporary Redirect)
B301 Moved Permanently
C307 Temporary Redirect
D200 OK with rewritten URI internally
Attempts:
2 left
💡 Hint
The 'redirect' flag sends a temporary redirect status code.
💻 Command Output
advanced
2:00remaining
What HTTP status code does the 'permanent' rewrite flag send?
Analyze this rewrite rule:
rewrite ^/site/(.*)$ /website/$1 permanent;

What status code will nginx send to the client?
Nginx
rewrite ^/site/(.*)$ /website/$1 permanent;
A303 See Other
B302 Found
C301 Moved Permanently
D200 OK with internal rewrite
Attempts:
2 left
💡 Hint
The 'permanent' flag sends a permanent redirect status code.
❓ Troubleshoot
expert
3:00remaining
Why does a rewrite with 'last' flag cause a redirect loop?
You have this nginx config snippet:
location /app/ {
  rewrite ^/app/(.*)$ /app/$1 last;
}

Requests to /app/page cause a redirect loop. What is the most likely reason?
Nginx
location /app/ {
  rewrite ^/app/(.*)$ /app/$1 last;
}
AThe rewrite rewrites the URI to the same URI, causing nginx to restart location search endlessly.
BThe 'last' flag is invalid here and causes nginx to crash.
CThe rewrite rule syntax is incorrect and nginx ignores it.
DThe 'break' flag should be used instead to avoid loops.
Attempts:
2 left
💡 Hint
Consider what happens when the rewritten URI matches the original location again.

Practice

(1/5)
1. What does the last flag do in an nginx rewrite directive?
easy
A. Stops processing the current location and restarts the rewrite phase with the new URI
B. Sends a temporary redirect (HTTP 302) to the client
C. Stops processing the rewrite directives and continues with the current location
D. Sends a permanent redirect (HTTP 301) to the client

Solution

  1. Step 1: Understand the last flag behavior

    The last flag tells nginx to stop processing current rewrite directives and restart the rewrite phase with the new URI.
  2. Step 2: Compare with other flags

    Unlike break which stops rewriting internally, last restarts processing, and unlike redirect or permanent which send redirects, it does not send a client redirect.
  3. Final Answer:

    Stops processing the current location and restarts the rewrite phase with the new URI -> Option A
  4. Quick Check:

    last = restart rewrite phase [OK]
Hint: Remember: last restarts rewrite, break stops internally [OK]
Common Mistakes:
  • Confusing last with break flag
  • Thinking last sends a redirect
  • Mixing last with permanent redirect
2. Which of the following is the correct syntax to perform a permanent redirect in nginx using the rewrite directive?
easy
A. rewrite ^/old$ /new break;
B. rewrite ^/old$ /new last;
C. rewrite ^/old$ /new permanent;
D. rewrite ^/old$ /new redirect;

Solution

  1. Step 1: Identify the flag for permanent redirect

    The permanent flag sends an HTTP 301 status code indicating a permanent redirect.
  2. Step 2: Check syntax correctness

    The syntax rewrite ^/old$ /new permanent; correctly applies the permanent redirect flag.
  3. Final Answer:

    rewrite ^/old$ /new permanent; -> Option C
  4. Quick Check:

    Permanent redirect uses permanent flag [OK]
Hint: Permanent redirect uses 'permanent' flag at end [OK]
Common Mistakes:
  • Using 'last' or 'break' instead of 'permanent' for redirects
  • Omitting the semicolon at the end
  • Confusing 'redirect' with 'permanent'
3. Given the nginx configuration snippet:
location /test {
  rewrite ^/test/(.*)$ /new/$1 redirect;
}

What will happen when a client requests /test/page?
medium
A. The client will receive a 301 permanent redirect to /new/page
B. The rewrite will restart processing with the new URI /new/page
C. The request will be internally rewritten to /new/page without redirect
D. The client will receive a 302 temporary redirect to /new/page

Solution

  1. Step 1: Understand the redirect flag

    The redirect flag sends an HTTP 302 temporary redirect to the client.
  2. Step 2: Analyze the rewrite rule

    The rewrite matches /test/page and rewrites it to /new/page with a temporary redirect.
  3. Final Answer:

    The client will receive a 302 temporary redirect to /new/page -> Option D
  4. Quick Check:

    redirect = 302 temporary redirect [OK]
Hint: redirect flag means 302 temporary redirect [OK]
Common Mistakes:
  • Confusing redirect with permanent
  • Thinking redirect rewrites internally
  • Assuming last or break flags here
4. You have this nginx rewrite rule:
rewrite ^/oldpath/(.*)$ /newpath/$1 last

But it causes a 500 Internal Server Error. What is the likely problem?
medium
A. Missing semicolon at the end of the rewrite directive
B. Using 'last' flag instead of 'break' causes infinite loop
C. The regex pattern is invalid
D. Rewrite directive must be inside a location block

Solution

  1. Step 1: Check syntax of rewrite directive

    Every nginx directive must end with a semicolon. The given rewrite lacks a semicolon.
  2. Step 2: Understand error cause

    Missing semicolon causes nginx to fail parsing config, leading to 500 error.
  3. Final Answer:

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

    Missing semicolon = syntax error = 500 error [OK]
Hint: Always end rewrite directives with semicolon [OK]
Common Mistakes:
  • Assuming last flag causes infinite loop
  • Ignoring missing semicolon syntax errors
  • Thinking regex pattern is wrong without checking
5. You want to rewrite URLs starting with /app to /app/index.html internally without redirecting the client, and stop further rewrite processing. Which rewrite directive and flag should you use?
hard
A. rewrite ^/app$ /app/index.html last;
B. rewrite ^/app$ /app/index.html break;
C. rewrite ^/app$ /app/index.html redirect;
D. rewrite ^/app$ /app/index.html permanent;

Solution

  1. Step 1: Identify internal rewrite without client redirect

    The break flag stops rewrite processing and serves the rewritten URI internally without redirecting the client.
  2. Step 2: Match requirement to flag

    Using break with the rewrite stops further rewrites and serves /app/index.html internally.
  3. Final Answer:

    rewrite ^/app$ /app/index.html break; -> Option B
  4. Quick Check:

    break = internal rewrite stop [OK]
Hint: Use break to rewrite internally and stop further rewrites [OK]
Common Mistakes:
  • Using last which restarts rewrite phase
  • Using redirect or permanent which send client redirects
  • Not stopping rewrite processing causing unexpected behavior
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