Bird
Raised Fist0
Nginxdevops~30 mins

Blue-green deployment routing in Nginx - Mini Project: Build & Apply

Choose your learning style10 modes available
Blue-green deployment routing with Nginx
📖 Scenario: You are managing a web service that needs zero downtime during updates. To do this, you use a blue-green deployment strategy. This means you have two versions of your app running: blue and green. You want to configure Nginx to route all user traffic to only one version at a time.
🎯 Goal: Build an Nginx configuration that routes all incoming requests to either the blue or green backend server based on a variable. This will let you switch traffic between versions easily.
📋 What You'll Learn
Create an upstream block for blue servers
Create an upstream block for green servers
Add a variable $active_version to select which upstream to use
Configure the server block to proxy requests to the selected upstream
Print the active version in the response header for verification
💡 Why This Matters
🌍 Real World
Blue-green deployment routing is used in production to update applications without downtime. It allows switching user traffic between two identical environments safely.
💼 Career
Understanding how to configure Nginx for blue-green deployments is valuable for DevOps engineers and site reliability engineers who manage continuous delivery pipelines and high-availability services.
Progress0 / 4 steps
1
Create upstream blocks for blue and green servers
Create two upstream blocks named blue_backend and green_backend. Each should have one server: blue.example.com:8080 for blue and green.example.com:8080 for green.
Nginx
Hint

Use the upstream directive to define backend groups.

2
Add a variable to select the active backend
Add a variable $active_version and set it to blue_backend to start routing traffic to the blue version.
Nginx
Hint

Use the map directive to set $active_version to blue_backend by default.

3
Configure server block to proxy to the active backend
Create a server block listening on port 80. Inside it, add a location / block that proxies requests to the backend selected by $active_version using proxy_pass http://$active_version;. Also add a header X-Active-Version with the value of $active_version.
Nginx
Hint

Use proxy_pass with the variable $active_version to route traffic dynamically.

4
Print the active version header for verification
Add a line to print the value of the X-Active-Version header in the response. Then reload Nginx and test by sending a request to port 80. The output should include the header X-Active-Version: blue_backend.
Nginx
Hint

Use curl -I http://localhost to see response headers including X-Active-Version.

Practice

(1/5)
1. What is the main purpose of using blue-green deployment routing with Nginx?
easy
A. To encrypt traffic between client and server
B. To increase the number of servers in the cluster
C. To cache static files for faster loading
D. To switch traffic between two app versions without downtime

Solution

  1. Step 1: Understand blue-green deployment concept

    Blue-green deployment uses two versions of an app (blue and green) to switch traffic smoothly.
  2. Step 2: Identify Nginx role in routing

    Nginx switches traffic by changing proxy_pass between blue and green upstreams, avoiding downtime.
  3. Final Answer:

    To switch traffic between two app versions without downtime -> Option D
  4. Quick Check:

    Blue-green routing = traffic switch without downtime [OK]
Hint: Blue-green means two versions, switch traffic smoothly [OK]
Common Mistakes:
  • Confusing blue-green with load balancing
  • Thinking it increases server count automatically
  • Assuming it handles encryption or caching
2. Which Nginx directive is used to switch traffic between blue and green app versions in blue-green deployment?
easy
A. proxy_pass
B. listen
C. server_name
D. root

Solution

  1. Step 1: Identify directive for routing requests

    The proxy_pass directive tells Nginx where to send incoming requests.
  2. Step 2: Understand blue-green switching

    Switching proxy_pass between blue and green upstream blocks changes the app version served.
  3. Final Answer:

    proxy_pass -> Option A
  4. Quick Check:

    proxy_pass switches traffic in blue-green [OK]
Hint: proxy_pass directs traffic to app versions [OK]
Common Mistakes:
  • Using listen to switch app versions
  • Confusing server_name with routing
  • Using root which sets document root, not proxy
3. Given this Nginx config snippet, what backend will receive traffic?
upstream blue {
  server 10.0.0.1:8080;
}
upstream green {
  server 10.0.0.2:8080;
}
server {
  listen 80;
  location / {
    proxy_pass http://green;
  }
}
medium
A. 10.0.0.1:8080 (blue)
B. 10.0.0.2:8080 (green)
C. Both blue and green servers
D. No backend, config error

Solution

  1. Step 1: Check proxy_pass target

    The proxy_pass points to http://green, so traffic goes to green upstream.
  2. Step 2: Identify green upstream server

    Green upstream has server 10.0.0.2:8080, so this server receives traffic.
  3. Final Answer:

    10.0.0.2:8080 (green) -> Option B
  4. Quick Check:

    proxy_pass to green = 10.0.0.2:8080 [OK]
Hint: proxy_pass target = backend server [OK]
Common Mistakes:
  • Assuming traffic goes to blue by default
  • Thinking both upstreams get traffic simultaneously
  • Believing config has syntax errors
4. Identify the error in this Nginx blue-green deployment config snippet:
upstream blue {
  server 10.0.0.1:8080;
}
upstream green {
  server 10.0.0.2:8080
}
server {
  listen 80;
  location / {
    proxy_pass http://blue;
  }
}
medium
A. Missing semicolon after green server line
B. proxy_pass should use https instead of http
C. listen directive must specify IP address
D. upstream block names must be uppercase

Solution

  1. Step 1: Check upstream block syntax

    The green upstream server line lacks a semicolon at the end, causing syntax error.
  2. Step 2: Validate other directives

    proxy_pass with http is valid; listen can omit IP; upstream names can be lowercase.
  3. Final Answer:

    Missing semicolon after green server line -> Option A
  4. Quick Check:

    Missing semicolon causes config error [OK]
Hint: Check semicolons after each directive line [OK]
Common Mistakes:
  • Assuming http must be https in proxy_pass
  • Thinking listen needs IP address
  • Believing upstream names must be uppercase
5. You want to switch live traffic from blue to green app version with zero downtime using Nginx. Which is the best approach?
hard
A. Stop blue servers and start green servers without changing Nginx
B. Change DNS to point to green servers and wait for propagation
C. Change proxy_pass in Nginx config from blue to green and reload Nginx
D. Restart Nginx without changing config to refresh connections

Solution

  1. Step 1: Understand zero downtime switch

    Changing proxy_pass in Nginx config directs traffic to green without downtime.
  2. Step 2: Reload Nginx to apply changes

    Reloading Nginx applies new config smoothly without dropping connections.
  3. Final Answer:

    Change proxy_pass in Nginx config from blue to green and reload Nginx -> Option C
  4. Quick Check:

    proxy_pass switch + reload = zero downtime [OK]
Hint: Switch proxy_pass and reload Nginx for smooth update [OK]
Common Mistakes:
  • Stopping blue servers causes downtime
  • Relying on DNS changes delays switch
  • Restarting Nginx without config change does nothing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard