Today, i’m going to explain how to install HAProxy as a Layer 7 load balancer for nginx on centos 7 /RHEL 7.
HAProxy:
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites and powers quite a number of the world’s most visited ones.
HAPproxy Algotithms:
-Round-Robin (roundrobin)
-Least-Connection (leastconn)
-Source (source)
-URI (uri)
-…
Environment:
In this tutorial i’m going to use 3 Centos 7 servers:
– HAProxy Load Balancer.
– 2 nginx Webserver nodes.
Step 1 – Install and Configure Nginx
In this step we need to install nginx in the two server nodes
[root@webserver01 ~]# yum update [root@webserver01 ~]# yum install nginx -y [root@webserver01 ~]# systemctl enable && systemctl start nginx [root@webserver01 ~]# systemctl status nginx [root@webserver01 ~]# firewall-cmd --permanent --add-service=http [root@webserver01 ~]# firewall-cmd --reload
Nginx now is installed. Go to the web directory of Nginx and change the index file so that we can see which of the two nodes is working:
[root@webserver01 ~]# echo " Welcome to my first web server" > /src/share/nginx/html/index.html [root@webserver02 ~]# echo " Welcome to my Second web server" > /src/share/nginx/html/index.html
After we finished the configuration, we need now to reload nginx on both nodes.
[root@webserver01 ~]# systemctl reload nginx
Step 2 – Install and Configure HAProxy
[root@haproxy ~]# yum install haproxy -y [root@haproxy ~]# systemctl enable haproxy && systemctl start haproxy
-Now let’s move to add our backend and frontend configuration under haproxy.cfg file
[root@haproxy ~]# vi /etc/haproxy/haproxy.cfg
First think to do is to add Frontend settings:
#--------------------------------------------------------------------- # Frontend #--------------------------------------------------------------------- frontend web_cluster bind *:80 #stats uri /haproxy?stats default_backend web_cluster_http
Second thing to do is to add the Backend Settings:
#--------------------------------------------------------------------- # round robin balancing backend HTTP #--------------------------------------------------------------------- backend web_cluster_http balance roundrobin # balance leastconn mode http server webserver01 192.168.1.71:80 check server webserver02 192.168.1.72:80 check #
Finaly, you can also enable the “stats page” section to allow you to monitor the status of the proxy and dont forget to open that port in the firewall, here we choose the port 9000 and we set a user and password to acces to “stats page” :
#--------------------------------------------------------------------- # Configuration for the Statistics Report #--------------------------------------------------------------------- listen stats bind :9000 stats enable stats hide-version stats refresh 20s stats show-node stats uri /stats stats auth username:password
After you finish, exit and save the file and finally don’t forget to reload the haproxy service.
Finally u can accces to Stats page by puting this link in your browser http://load_balancer_ip:9000/stats.
2 comments
hello i want this config, + naxis but i have to put naxis on the server HAPROXy or on ALL the web servers nginx (2 times)
Hi,
Could you explain what your exigencies like this we can help you.