Balancing load with NGINX
Now that we have a pool of Docker applications to forward traffic to, we can prepare our load balancer. In this section, we will briefly cover NGINX, a popular web server that has high concurrency and performance. It is commonly used as a reverse proxy to forward requests to more dynamic web applications, such as the Node.js one we wrote earlier. By configuring NGINX to have multiple reverse proxy destinations, such as our pool of Docker applications, it will balance the load of requests coming to it across the pool.
The following steps will deploy NGINX to our Docker Swarm cluster for load balancing:
- First, let's prepare an NGINX configuration file called
nginx.conf
:
events { } http { resolver 127.0.0.11 valid=5s ipv6=off; server { listen 80; location / { set $backend application; proxy_pass http://$backend:8000; } } }
Note that we used NGINX's DNS service discovery to send traffic to our application via its network alias application...