0
0
Nginxdevops~10 mins

Rewrite flags (last, break, redirect, permanent) in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Rewrite flags (last, break, redirect, permanent)
Request comes in
Rewrite rule matches?
NoServe original URI
Yes
Apply rewrite with flag
last
break
redirect
permanent
When a rewrite rule matches, nginx applies the flag to decide how to handle the request next: restart, stop, or redirect.
Execution Sample
Nginx
rewrite ^/oldpath/(.*)$ /newpath/$1 last;
rewrite ^/temp/(.*)$ /tempfile break;
rewrite ^/go/(.*)$ http://example.com/$1 redirect;
rewrite ^/perm/(.*)$ http://example.com/$1 permanent;
Four rewrite rules showing different flags: last restarts location search, break stops rewriting, redirect sends 302, permanent sends 301.
Process Table
StepRequest URIRewrite Rule MatchedFlagAction TakenResulting URI or Response
1/oldpath/page^/oldpath/(.*)$lastRestart location search with /newpath/page/newpath/page
2/newpath/pageNo match-Serve /newpath/page/newpath/page
3/temp/file^/temp/(.*)$breakStop rewriting, continue with /tempfile/tempfile
4/tempfileNo rewrite-Serve /tempfile/tempfile
5/go/home^/go/(.*)$redirectSend 302 redirect to http://example.com/home302 Redirect to http://example.com/home
6/perm/about^/perm/(.*)$permanentSend 301 redirect to http://example.com/about301 Redirect to http://example.com/about
7/otherNo match-Serve /other/other
💡 Execution stops when no rewrite rule matches or after redirect response is sent.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 6Final
Request URI/oldpath/page/newpath/page/temp/file/go/home/perm/about/other
Rewrite Rule MatchedNone^/oldpath/(.*)$^/temp/(.*)$^/go/(.*)$^/perm/(.*)$None
Action TakenNoneRestart location searchStop rewriting302 redirect301 redirectServe original
Key Moments - 3 Insights
Why does the 'last' flag cause nginx to restart location search?
Because 'last' tells nginx to treat the rewritten URI as a new request and search for matching location blocks again, as shown in step 1 and 2 of the execution_table.
What happens when the 'break' flag is used in a rewrite?
'break' stops further rewrite processing and continues with the current location using the rewritten URI, as seen in step 3 and 4.
How do 'redirect' and 'permanent' flags differ in their HTTP response?
'redirect' sends a temporary 302 redirect, while 'permanent' sends a 301 permanent redirect, demonstrated in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Request URI after step 1?
A/oldpath/page
B/temp/file
C/newpath/page
D/go/home
💡 Hint
Check the 'Request URI' column in row for step 1.
At which step does nginx send a 301 redirect response?
AStep 6
BStep 5
CStep 3
DStep 2
💡 Hint
Look at the 'Action Taken' and 'Resulting URI or Response' columns for redirect types.
If the 'break' flag was replaced with 'last' in step 3, what would happen?
ANginx would serve /temp/file without rewriting
BNginx would restart location search with /tempfile
CNginx would send a redirect to /tempfile
DNginx would stop processing and serve original URI
💡 Hint
Compare the behavior of 'break' vs 'last' flags in the concept_flow and execution_table.
Concept Snapshot
nginx rewrite flags control request handling:
- last: rewrite and restart location search
- break: rewrite and stop rewriting, continue current location
- redirect: send 302 temporary redirect
- permanent: send 301 permanent redirect
Use flags to control URL rewriting and client response.
Full Transcript
When nginx receives a request, it checks rewrite rules. If a rule matches, it applies the rewrite and uses the flag to decide what to do next. The 'last' flag restarts location search with the new URI, allowing further processing. The 'break' flag stops rewriting and continues with the current location using the rewritten URI. The 'redirect' flag sends a temporary 302 redirect to the client, while 'permanent' sends a 301 permanent redirect. If no rewrite matches, nginx serves the original URI. This flow helps control how URLs are rewritten and how clients are redirected.