How does Nginx handle redirects with return and rewrite?
Learn how Nginx handles redirects with return and rewrite: 301 vs 302, permanent and redirect flags, internal rewrites, and HTTPS examples.
Expected Interview Answer
Nginx performs redirects with two directives: return, which immediately sends a status code and Location header, and rewrite, which changes the request URI using a regex and can either continue processing internally or issue an external redirect.
return 301 https://$host$request_uri; is the preferred, fastest way to send a permanent redirect because it stops processing at once. rewrite ^/old/(.*)$ /new/$1 permanent; matches a pattern, substitutes a new URI, and the flag decides the outcome: permanent (301) and redirect (302) send the browser elsewhere, while last and break rewrite internally without a client round trip. Use return for simple whole-host or path redirects and rewrite when you need regex capture groups.
- return is fast and stops processing immediately
- rewrite supports regex capture and substitution
- Clear control over 301 vs 302 semantics
- Internal rewrites avoid extra client round trips
- Keeps SEO intact with correct permanent redirects
AI Mentor Explanation
return is like an umpire raising the finger the instant a decision is clear — play stops and everyone acts on the ruling at once. rewrite is like the third umpire re-examining the delivery frame by frame, adjusting the call using detailed evidence, then either resuming play internally or signaling a formal change. Nginx uses return for an instant verdict and rewrite when pattern analysis of the URI is needed first.
Step-by-Step Explanation
Step 1
Pick return for simple redirects
Use return 301 https://$host$request_uri; to redirect a whole host or fixed path immediately and efficiently.
Step 2
Choose the right status
301 (permanent) preserves SEO and is cached; 302 (temporary) is not — match the code to intent.
Step 3
Use rewrite for regex logic
Write rewrite ^/old/(.*)$ /new/$1 permanent; to match a pattern and reuse captured groups in the new URI.
Step 4
Understand rewrite flags
permanent = 301 and redirect = 302 send the client away; last and break rewrite internally with no client round trip.
Step 5
Test and reload
Validate with nginx -t and reload; verify the response with curl -I to confirm the status and Location header.
What Interviewer Expects
- Knows return sends an immediate status and Location
- Knows rewrite matches a regex and substitutes the URI
- Explains 301 vs 302 semantics
- Distinguishes permanent/redirect from last/break flags
- Prefers return for simple whole-host redirects
- Mentions verifying with curl -I
Common Mistakes
- Using rewrite for simple redirects where return is faster
- Confusing 301 and 302, hurting SEO
- Expecting last or break to redirect the browser
- Forgetting the scheme/host so the Location header is relative and wrong
- Chaining unnecessary rewrites that cause redirect loops
Best Answer (HR Friendly)
“Nginx sends visitors to a new address in two ways: return immediately forwards them with a status code, and rewrite changes the web address using pattern rules before forwarding or continuing. return suits simple moves, while rewrite handles cases that need matching parts of the URL.”
Code Example
# Fast permanent redirect of an entire host to HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
# Regex redirect that reuses a captured path segment
location /old/ {
rewrite ^/old/(.*)$ /new/$1 permanent;
}Follow-up Questions
- When would you use rewrite instead of return?
- What is the difference between the last and break rewrite flags?
- Why is 301 better than 302 for a permanent move?
- How do you redirect all HTTP traffic to HTTPS?
- How can rewrites cause redirect loops and how do you avoid them?
MCQ Practice
1. Which directive is the fastest way to redirect an entire host?
return sends the status and Location immediately and stops processing, making it ideal for whole-host redirects.
2. Which rewrite flag issues a 301 external redirect?
The permanent flag returns HTTP 301, while redirect returns 302 and last/break rewrite internally.
3. What do the last and break rewrite flags do?
last and break change the URI internally and continue processing without redirecting the browser.
Flash Cards
What does return do in Nginx? — Immediately sends a status code and Location header, stopping further processing — best for simple redirects.
What does rewrite do? — Matches the URI with a regex and substitutes a new one; flags decide internal rewrite vs external redirect.
permanent vs redirect flag? — permanent returns 301 (cached, SEO-friendly); redirect returns 302 (temporary).
last vs break flags? — Both rewrite the URI internally without redirecting the client; they differ in how location re-evaluation continues.