An example config for an NGINX Load Balancer.
- Load balancing across multiple backend nodes using the
least_connstrategy. - Health check endpoint at
/health. - NGINX status endpoint at
/status(access from localhost and LAN only). - Easy to extend to cover any use case.
- Docker Compose setup with mock backend nodes for testing.
-
Clone the repo:
git clone https://github.com/sanbotto/nginx-lb.git cd nginx-lb -
Start the services:
docker-compose up -d
-
Test the LB:
-
Go to http://localhost:8080/ from your browser or use
curl:curl http://localhost:8080/
You should see responses from "Mock Node 1", "Mock Node 2", or "Mock Node 3" (rotated by the load balancer, round-robin stylez).
-
Health check:
curl http://localhost:8080/health
-
NGINX status:
curl http://localhost:8080/status
-
.
├── docker-compose.yml
├── Dockerfile
├── nginx.conf
└── README.md
- docker-compose.yml: Testing grounds for the load balancer and mock backend services.
- Dockerfile: Builds a custom NGINX image with health checks.
- nginx.conf: NGINX configuration with all the good stuff (LB, proxy, basic metrics and health check endpoint).
- Upstreams: 3 mock HTTP servers are deployed with
docker-composein order to test the LB. Each one has a different port since this is deployed localy and you can't map the same port twice. When working when real upstreams, you'll most likely run each service separately and will be able to use the same port if needed. - Network: A custom network is created and all containers are linked to it so that traffic can flow properly.
- To load balance your own services, update the
upstreamblock innginx.confwith the proper endpoints. - Adjust the allowed IPs for
/statusas needed.
MIT License
Note: This project is for development and testing purposes. It was simply a quick config that I wanted to try. I'm mostly uploading this to GitHub for my own future reference, but it also might help someone else, and that's why I'm making it public.