This repo demonstartes how ngnix server can be used as a load balancer and how client request can be balanced by distributing incoming client requests across multiple backend servers.
- Three instances of a FastAPI app (app1, app2, app3) run inside separate Docker containers.
- Each container runs Uvicorn on port 8000 internally.
- Nginx is configured to balance incoming requests across the three containers.
- Nginx communicates with containers using Docker service names (app1, app2, app3) and container port 8000.
- Host ports (like 8001, 8002, 8003) are mapped for external access if needed, but Nginx uses internal ports for load balancing.
upstream app {
server app1:8000;
server app2:8000;
server app3:8000;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://app;
}
}
- Manages all containers together: FastAPI apps and Nginx.
- A Python script (test_load.py) sends 10000 requests to Nginx (http://127.0.0.1:8000/).
- Each FastAPI container returns its hostname.
- The script counts how many requests each container handles:
{'0e526d71457f': 3334, '1fa129aab9fa': 3333, 'f601aba4acdf': 3333}
Requests are almost equally distributed, showing load balancing is working.
3.9.13git clone "https://github.com/Oudarja/Load-Balancing-with-Nginx-Server.git"python -m venv .venv
myenv\Scripts\activatepip install -r requirements.txtdocker compose up --build
docker logs nginx
python test.py