Nginx Load Balancer
For this blog post, nginx has been installed on 3 servers: webserver1, webserver2, and loadbalancer.
To configure an nginx server as a load balancer, edit the /etc/nginx/sites-enabled/default file, and configure an upstream servers block as follows:
upstream webserverz {
server webserver1;
server webserver2;
}
Then, configure the nginx server in the same configuration file to act as a reverse proxy server by editing the “location /” block to the following, with the upstream block’s name:
location / {
proxy_pass http://webserverz;
}
nginx plus is part of the commercial subscription that offers more capabilities, such as the health_check directive which is used to periodically run a health check on upstream web servers. There is the nginx_upstream_check module that can be used with the community edition of nginx for performing a health check.
Note: When not using health checks, stopping an EC2 instance running the web server and starting it again caused a new public IP address to get assigned. Nginx would hang when attempting to reach the now-unassigned public IP. The problem will likely not occur when using Elastic IPs (static IP / public IP address reservation).