Skip to content

sanbotto/nginx-lb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

nginx-lb

An example config for an NGINX Load Balancer.

Features

  • Load balancing across multiple backend nodes using the least_conn strategy.
  • 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.

Getting Started

Prerequisites

Quick Start

  1. Clone the repo:

    git clone https://github.com/sanbotto/nginx-lb.git
    cd nginx-lb
  2. Start the services:

    docker-compose up -d
  3. 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

Project Structure

.
├── 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).

Specifics of the testing env

  • Upstreams: 3 mock HTTP servers are deployed with docker-compose in 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.

Customization

  • To load balance your own services, update the upstream block in nginx.conf with the proper endpoints.
  • Adjust the allowed IPs for /status as needed.

License

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.

About

An example config for an NGINX Load Balancer

Resources

Stars

Watchers

Forks