- Alwin John
- April 30, 2021
Install and Configure Nginx as load-balancer in Linux.
Nginx is one of the powerful web servers which is used to host multiple websites or creating a reverse proxy/load-balancer or both. In most of the cases, a load-balancer is used to configure highly available web applications and to minimize the downtime due to any failure. In this blog, we are going to create a nginx server as loadbalancer which will serve users requests to backend server in round-robin sequence.
Prerequisites.
- 3 VMs (2 web-servers and 1 nginx).
- Connectivity between all 3 servers. No firewall rule should block HTTP traffic from one VM to other VM.
- We assume that all three VMs are configured with below IP addresses.
- Nginx: 192.168.1.1
- Web-server-1 (apache): 192.168.1.2
- Web-server-2 (apache): 192.168.1.3
Steps to follow:
- Installing apache and configure a sample web page on both apache servers using below command.
- sudo yum install httpd -y && sudo echo “web-server-1” > /var/www/html/index.html && sudo systemctl start httpd.service
- Similarly, run the above command to second apache server.
- sudo yum install httpd -y && sudo echo “web-server-2” > /var/www/html/index.html && sudo systemctl start httpd.service
- Installing and configuring nginx server.
- sudo yum install nginx -y
4. Now create a new file in /etc/nginx/conf.d/lb.conf and add below lines in this file.
upstream web-servers {
server 192.168.1.2;
server 192.168.1.3;
}
server {
listen 80;
location / {
proxy_pass http://web-servers;
}
}
5. Restart nginx service.
- sudo systemctl restart nginx.service
6. Now you can request nginx IP(192.168.1.1) address in browser and you will see the web pages from apache servers in round-robin sequence.