What is the difference between rewrite and return in Nginx?
Learn the difference between rewrite and return in Nginx, when to use each, redirect flags, and how to avoid loops — with clear config examples.
Expected Interview Answer
In Nginx, return immediately stops request processing and sends a fixed status code or redirect, while rewrite changes the request URI (optionally with a regex) and usually lets processing continue so location matching can run again.
return is simple, fast, and unconditional — it ends the request right where it is written, making it the preferred tool for permanent redirects and canonical host handling. rewrite matches the URI against a regular expression and substitutes a new path; without the permanent or redirect flag it rewrites internally and re-evaluates locations, which can loop or add overhead. Because rewrite runs regex per request and may re-scan locations, official guidance is to prefer return for straightforward redirects.
- return is faster with no regex cost
- return makes redirect intent explicit and readable
- rewrite supports pattern-based URI transformation
- rewrite can rewrite internally without a browser round-trip
- Choosing correctly avoids redirect loops
AI Mentor Explanation
Think of an umpire at the crease. A return is like signalling an outright decision — the umpire raises a finger, the batter is out, and that delivery is finished with no further debate. A rewrite is like the third umpire quietly correcting the scorecard entry and sending play on: the ball is relabelled, but the over continues and later checks still apply. One ends the appeal instantly; the other edits the record and lets the game roll forward.
Step-by-Step Explanation
Step 1
Reach for return first
For a plain redirect or fixed response, use return 301 https://$host$request_uri; — it is explicit and fast.
Step 2
Use rewrite for pattern changes
When the new URI depends on a regex capture, use rewrite ^/old/(.*)$ /new/$1 to transform the path.
Step 3
Pick the right flag
Add permanent (301) or redirect (302) to send the browser; use last or break to rewrite internally without a round-trip.
Step 4
Understand re-evaluation
rewrite ... last restarts location matching with the new URI; break stops rewrite processing in the current block.
Step 5
Guard against loops
Ensure a rewritten URI cannot match the same rewrite again, or Nginx returns 500 after too many internal redirects.
What Interviewer Expects
- Knows return ends processing immediately
- Knows rewrite transforms the URI via regex
- Understands last vs break vs permanent vs redirect flags
- Explains why return is preferred for simple redirects
- Aware of internal redirect loops and the 500 risk
Common Mistakes
- Using rewrite for a simple redirect that return handles better
- Confusing the last and break flags
- Forgetting permanent, so a redirect defaults to internal rewrite
- Writing a rewrite that matches its own output and loops
- Assuming rewrite always sends the browser a new URL
Best Answer (HR Friendly)
“In Nginx, return is like immediately telling a visitor 'go to this address' and ending the conversation, while rewrite quietly edits the address they asked for and keeps handling the request. You use return for simple redirects and rewrite when the new path is built from a pattern.”
Code Example
# Simple, preferred redirect with return
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
# Pattern-based path change with rewrite
server {
location /old/ {
rewrite ^/old/(.*)$ /new/$1 permanent;
}
# Internal rewrite, no browser round-trip
location /app/ {
rewrite ^/app/(.*)$ /index.php?path=$1 last;
}
}Follow-up Questions
- When would you choose rewrite over return?
- What is the difference between the last and break flags?
- Why can a rewrite cause a 500 internal redirect loop?
- How do you do a permanent versus temporary redirect in Nginx?
- What does $request_uri contain compared to $uri?
MCQ Practice
1. Which directive stops request processing immediately and sends a fixed status?
return ends processing at once and emits the given code or redirect; rewrite transforms the URI and usually continues.
2. What does the last flag on a rewrite do?
last ends the current rewrite phase and re-runs location matching against the rewritten URI.
3. For a plain permanent redirect of a whole host, the recommended directive is?
Official Nginx guidance prefers return 301 for simple redirects because it is faster and clearer than rewrite.
Flash Cards
What does return do? — Stops processing immediately and sends a fixed status code, text, or redirect URL.
What does rewrite do? — Matches the URI against a regex and substitutes a new URI, optionally continuing processing.
rewrite last vs break? — last re-runs location matching with the new URI; break stops rewrite processing in the current block.
Why prefer return for redirects? — It avoids regex cost and location re-evaluation, and states intent clearly.