Understanding Nginx Rewrite Flags: last, break, redirect, permanent
📖 Scenario: You are managing a website using Nginx web server. You want to control how URLs are rewritten and redirected to improve user experience and SEO.Different rewrite flags in Nginx change how the server handles URL changes. Learning these flags helps you control page redirects and URL rewriting effectively.
🎯 Goal: Build a simple Nginx configuration that uses the rewrite directive with the flags last, break, redirect, and permanent to see how each flag affects URL handling.
📋 What You'll Learn
Create a basic Nginx server block with a root location
Add rewrite rules using last, break, redirect, and permanent flags
Test each rewrite rule by simulating requests
Print or log the final rewritten URL or redirect status
💡 Why This Matters
🌍 Real World
Web servers often need to rewrite URLs to organize content better or redirect users to updated pages. Understanding rewrite flags helps manage these changes smoothly.
💼 Career
DevOps engineers and system administrators use Nginx rewrite rules to optimize web traffic, improve SEO, and maintain site structure without breaking links.
Progress0 / 4 steps
1
Create a basic Nginx server block
Write an Nginx server block listening on port 80 with a root directory set to /var/www/html. Inside the server block, create a location / block that serves files from the root directory.
Nginx
Hint
Use try_files $uri $uri/ =404; inside location / to serve files or return 404 if not found.
2
Add a rewrite rule with the last flag
Inside the location / block, add a rewrite rule that matches URLs starting with /oldpath and rewrites them to /newpath using the last flag.
Nginx
Hint
The last flag tells Nginx to restart the location search after rewriting the URL.
3
Add rewrite rules with break, redirect, and permanent flags
Add three more rewrite rules inside the location / block: one that rewrites /breakpath to /breaktarget using the break flag; one that redirects /redirectpath to /redirecttarget with a temporary redirect using the redirect flag; and one that redirects /permanentpath to /permanenttarget with a permanent redirect using the permanent flag.
Nginx
Hint
The break flag stops processing rewrite directives in the current location. The redirect flag sends a temporary redirect (302). The permanent flag sends a permanent redirect (301).
4
Print the final rewritten URL or redirect status
Add a return 200 directive inside the location / block that returns the rewritten URI using the variable $uri so you can see the final URL after rewrite rules are applied.
Nginx
Hint
The return 200 "$uri"; directive sends the current URI back in the response body so you can see the effect of rewrite rules.
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
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.
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.
Final Answer:
Stops processing the current location and restarts the rewrite phase with the new URI -> Option A
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
Step 1: Identify the flag for permanent redirect
The permanent flag sends an HTTP 301 status code indicating a permanent redirect.
Step 2: Check syntax correctness
The syntax rewrite ^/old$ /new permanent; correctly applies the permanent redirect flag.
Final Answer:
rewrite ^/old$ /new permanent; -> Option C
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
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
Step 1: Understand the redirect flag
The redirect flag sends an HTTP 302 temporary redirect to the client.
Step 2: Analyze the rewrite rule
The rewrite matches /test/page and rewrites it to /new/page with a temporary redirect.
Final Answer:
The client will receive a 302 temporary redirect to /new/page -> Option D
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
Step 1: Check syntax of rewrite directive
Every nginx directive must end with a semicolon. The given rewrite lacks a semicolon.
Step 2: Understand error cause
Missing semicolon causes nginx to fail parsing config, leading to 500 error.
Final Answer:
Missing semicolon at the end of the rewrite directive -> Option A
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
Step 1: Identify internal rewrite without client redirect
The break flag stops rewrite processing and serves the rewritten URI internally without redirecting the client.
Step 2: Match requirement to flag
Using break with the rewrite stops further rewrites and serves /app/index.html internally.
Final Answer:
rewrite ^/app$ /app/index.html break; -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.