
Setting Up a Local Load Balancer Using NGINX and Flask on WSL
Imagine you’re running a busy website, and suddenly, traffic spikes. One server can’t handle it all—requests slow down, and users get frustrated. That’s where a load balancer comes in, distributing traffic across multiple servers to keep things running smoothly. In this guide, we’ll set up a simple local load balancer using NGINX and two Flask applications on WSL (Windows Subsystem for Linux). It’s a fantastic way to learn load balancing hands-on, right on your own machine!
Context
- NGINX: A high-performance web server that doubles as a reverse proxy and load balancer. It’s widely used for its speed and ability to manage traffic efficiently.
- Flask: A minimalist Python web framework perfect for building lightweight web apps or APIs.
- WSL: A feature of Windows that lets you run a Linux environment (like Ubuntu) without a separate machine or virtual machine, making it ideal for development.
By combining these tools, you’ll create a mini load-balanced system where NGINX directs traffic between two Flask servers. Let’s dive in!
🧰 Prerequisites
Before starting, ensure you have:
- WSL installed (Ubuntu recommended)
- Python 3
- Internet connection (for package downloads)
- Basic Linux command knowledge
📋 Step-by-Step Guide
✅ Step 1: Set Up Project Folder
Start by creating a folder to keep your project organized.
mkdir nginx-loadb-demo
cd nginx-loadb-demo
- Why?: A dedicated folder keeps your files tidy and easy to manage.
✅ Step 2: Create a Python Virtual Environment
Set up a virtual environment to isolate your project’s Python dependencies.
sudo apt update
sudo apt install python3-venv
python3 -m venv venv
source venv/bin/activate
- Explanation:
sudo apt update: Refreshes your package list.sudo apt install python3-venv: Installs the tool to create virtual environments.python3 -m venv venv: Creates a virtual environment namedvenv.source venv/bin/activate: Activates it, so your commands use this environment’s packages.
✅ Step 3: Install Flask
Install Flask, the framework for your backend servers.
pip install flask
- Why?: Flask lets you quickly build the web servers NGINX will balance.
✅ Step 4: Create Two Flask Backend Servers
Create two simple Flask apps to act as your backend servers.
- app1.py
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Response from Server 1" if __name__ == '__main__': app.run(host="0.0.0.0", port=5001) - app2.py
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Response from Server 2" if __name__ == '__main__': app.run(host="0.0.0.0", port=5002) - Explanation:
- Each app responds with a unique message at the root URL (
/). - They run on different ports (
5001and5002) to simulate separate servers.
- Each app responds with a unique message at the root URL (
✅ Step 5: Run the Flask Servers
Launch each Flask app in its own terminal so they run simultaneously.
- Terminal 1
source venv/bin/activate python app1.py - Terminal 2
source venv/bin/activate python app2.py - Why?: Running them separately ensures both servers are available for NGINX to balance.
✅ Step 6: Install and Configure NGINX
NGINX will distribute traffic between your Flask servers.
- Install NGINX
sudo apt install nginx - Edit the NGINX Config
Replace its contents with:sudo nano /etc/nginx/nginx.confevents {} http { upstream flask_backend { server 127.0.0.1:5001; server 127.0.0.1:5002; } server { listen 8080; location / { proxy_pass http://flask_backend; } } }- Save and Exit: Press
CTRL + O,ENTER, thenCTRL + X. - Explanation:
upstream flask_backend: Groups your Flask servers for load balancing.server: Sets NGINX to listen on port8080and forward requests to theflask_backendgroup.
- Save and Exit: Press
✅ Step 7: Restart or Reload NGINX
Apply your configuration changes.
- If NGINX is running:
sudo nginx -s reload - If NGINX isn’t running:
sudo nginx - Why?: Reloading updates NGINX without downtime; starting it kicks things off if it’s stopped.
✅ Step 8: Test the Load Balancer
Test your setup by sending requests to NGINX.
curl http://localhost:8080
Run it multiple times. Expected output alternates:
Response from Server 1
Response from Server 2
Response from Server 1
...
- Explanation: NGINX uses a round-robin method to alternate between servers, proving your load balancer works!
🎉 Conclusion
You’ve done it! You’ve built a local load balancer with NGINX and Flask on WSL, distributing traffic between two backend servers. This setup is a great foundation for understanding how load balancing works in real-world applications. Play around with it—tweak settings, add servers, or try the optional steps below!
🧪 Optional Next Steps
Take your setup further with these ideas:
- 🔁 Use
least_connmethod: Balances load based on the fewest active connections. - 🔍 Add health checks: Ensures NGINX only uses healthy servers.
- 📌 Enable sticky sessions (IP hash): Keeps a client tied to one server.
- 🐳 Containerize with Docker: Simplifies scaling and deployment.
Enjoy experimenting with your new load balancer!