Perfect π
Below is a FINAL, CLEAN README.md in pure GitHub Markdown format.
π You can directly copyβpaste this exactly as it is into your GitHub repo π No extra explanation, no chat text, no formatting issues π This is production-ready + interview-ready
# π Blue-Green Deployment Using Nginx (Zero Downtime)
This repository demonstrates a **zero-downtime Blue-Green deployment strategy** using **Nginx**, **PM2**, and **Node.js**, where traffic is seamlessly switched between two application versions without stopping the service.
---
## π Objective
- Run **two versions of the same API** (Blue & Green) in parallel
- Switch live traffic **without downtime**
- No application restart during deployment
- Traffic switches using **Nginx reload**
- Instant rollback capability
---
## π§ Architecture Overview
Client | v Nginx (Reverse Proxy - Port 8080) | |----> Green App (Port 5000) | |----> Blue App (Port 5001)
---
## π οΈ Tech Stack
| Component | Purpose |
|--------|--------|
| Node.js | Backend API |
| PM2 | Process manager |
| Nginx | Reverse proxy & traffic switch |
| systemd | Reload hook automation |
---
## π Project Structure
blue-green-poc/ βββ apps/ β βββ app-green.js β βββ app-blue.js βββ ecosystem.config.js βββ README.md
---
## βοΈ Prerequisites
- Ubuntu/Linux VM
- sudo access
- Node.js (via NVM recommended)
- PM2
- Nginx
---
## π§ Setup Instructions
### 1οΈβ£ Install Node.js (NVM)
```bash
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
Verify:
node -v
npm -vnpm install -g pm2
pm2 -vsudo apt update
sudo apt install -y nginxconst express = require("express");
const app = express();
app.get("/hello", (req, res) => {
res.json({ message: "Hello from GREEN (5000)" });
});
app.listen(5000);const express = require("express");
const app = express();
app.get("/hello", (req, res) => {
res.json({ message: "Hello from BLUE (5001)" });
});
app.listen(5001);npm init -y
npm install expressmodule.exports = {
apps: [
{ name: "hello-green", script: "./apps/app-green.js" },
{ name: "hello-blue", script: "./apps/app-blue.js" }
]
};Start applications:
pm2 start ecosystem.config.js
pm2 listTest:
curl http://localhost:5000/hello
curl http://localhost:5001/hellosudo rm /etc/nginx/sites-enabled/defaultsudo mkdir -p /etc/nginx/statesudo nano /etc/nginx/state/backend_upstream.confserver 127.0.0.1:5000;sudo nano /etc/nginx/conf.d/backend.confupstream backend_app {
include /etc/nginx/state/backend_upstream.conf;
}sudo nano /etc/nginx/sites-available/blue-green.confserver {
listen 8080;
server_name _;
location / {
proxy_pass http://backend_app;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Enable site:
sudo ln -s /etc/nginx/sites-available/blue-green.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxTest:
curl http://localhost:8080/hellowhile true; do curl -s http://localhost:8080/hello; sleep 0.5; donesudo nano /etc/nginx/state/backend_upstream.confChange to:
server 127.0.0.1:5001;Reload Nginx:
sudo nginx -s reloadβ Traffic switches with zero downtime
sudo nano /usr/local/bin/nginx-blue-green-switch.sh#!/bin/bash
STATE="/etc/nginx/state/active_port"
UPSTREAM="/etc/nginx/state/backend_upstream.conf"
CURRENT=$(cat $STATE)
if [ "$CURRENT" = "5000" ]; then
echo "5001" > $STATE
echo "server 127.0.0.1:5001;" > $UPSTREAM
else
echo "5000" > $STATE
echo "server 127.0.0.1:5000;" > $UPSTREAM
fisudo chmod +x /usr/local/bin/nginx-blue-green-switch.shsudo systemctl edit nginx[Service]
ExecReload=
ExecReload=/usr/local/bin/nginx-blue-green-switch.sh
ExecReload=/usr/sbin/nginx -s reloadApply changes:
sudo systemctl daemon-reexecsudo systemctl reload nginxβ Automatic traffic switch β Zero downtime β No failed requests
Rollback is instant:
sudo systemctl reload nginxTwo application versions run in parallel and Nginx reload switches traffic instantly with zero downtime.