0
0
Nginxdevops~10 mins

Conditional redirects with if in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Conditional redirects with if
Request Received
Evaluate if condition
Redirect
End Request
Nginx checks the if condition on each request. If true, it redirects; if false, it serves the request normally.
Execution Sample
Nginx
server {
  listen 80;
  if ($host = 'old.example.com') {
    return 301 https://new.example.com$request_uri;
  }
}
Redirect requests from old.example.com to new.example.com preserving the path.
Process Table
StepRequest HostCondition ($host = 'old.example.com')ActionResult
1old.example.comTruereturn 301 redirectRedirect to https://new.example.com$request_uri
2new.example.comFalseserve normallyServe requested content
3other.example.comFalseserve normallyServe requested content
💡 Requests with host 'old.example.com' redirect; others serve normally.
Status Tracker
VariableStartRequest 1Request 2Request 3
$hostundefinedold.example.comnew.example.comother.example.com
Condition ResultundefinedTrueFalseFalse
Key Moments - 2 Insights
Why does the redirect only happen for 'old.example.com' and not others?
Because the if condition checks if $host equals 'old.example.com'. Only when this is true (see execution_table row 1) does nginx perform the redirect.
What happens if the condition is false?
Nginx skips the redirect and serves the request normally, as shown in execution_table rows 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the action taken when the request host is 'new.example.com'?
AServe normally
BRedirect to new.example.com
CReturn 404 error
DRedirect to old.example.com
💡 Hint
Check execution_table row 2 under 'Action' column.
At which step does the condition become true?
AStep 2
BStep 1
CStep 3
DNone
💡 Hint
Look at the 'Condition' column in execution_table.
If the condition was changed to check for 'new.example.com', what would happen at step 1?
ARedirect occurs
BError occurs
CServe normally
DRequest is blocked
💡 Hint
Refer to variable_tracker and execution_table logic for condition evaluation.
Concept Snapshot
Nginx conditional redirect syntax:
if (condition) {
  return 301 https://newsite$request_uri;
}
Redirects only if condition is true.
Otherwise, serves request normally.
Use for simple host or path based redirects.
Full Transcript
This visual execution shows how nginx uses the if directive to conditionally redirect requests. When a request arrives, nginx checks if the host matches 'old.example.com'. If yes, it sends a 301 redirect to 'https://new.example.com' preserving the path. If not, it serves the request normally. The execution table traces three requests with different hosts, showing when the redirect happens and when it does not. The variable tracker shows the $host value and condition result for each request. Key moments clarify why only matching hosts redirect and what happens otherwise. The quiz tests understanding of condition evaluation and redirect behavior.