-->

Setting up HaProxy

How to setup and Install Haproxy..

Load balancer for servers 214bb

HaProxy is by far the fastest and best light weight load balancer around in my book.  I have been using it for years and will always continue  to use it so I have decided to share my installation process and example config files. Nowadays websites need a 99.999% up time which just isn’t possible with 1 standalone server

CentOS 6 already has  HAProxy packages available under base repository

On CentOS/RHEL 5:

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

Install HAProxy package using yum package manager from command line interface using following comamnd.

# yum install haproxy

Configuiring Haproxy

The config file for setting up and getting your loadbalancer (Haproxy) working is located in /etc/haproxy/haproxy.cfg

nano /etc/haproxy/haproxy.cfg

#———————————————————————
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#———————————————————————

#———————————————————————
# Global settings
#———————————————————————
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the ‘-r’ option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    user        haproxy
    group       haproxy
    daemon
       nbproc  1

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#———————————————————————
# common defaults that all the ‘listen’ and ‘backend’ sections will
# use if not designated in their block
#———————————————————————
defaults

#tune.chksize 36384
 log global
 mode http
 option httplog
 option dontlognull
 retries 3
 balance roundrobin
 option redispatch
 option forwardfor
 option forceclose        # Slows the connection
 maxconn 10000
 contimeout 30000
 clitimeout 40000
 srvtimeout 55000
 stats enable     # enables you to see the stats from a webpage
 stats auth admin:98!n567ehny!&*

#———————————————————————
# main frontend which proxys to the backends
#———————————————————————

option httpchk GET /index.php HTTP/1.1\r\nHost:\ www.cyfordtechnologies.com

frontend all 0.0.0.0:80 # Listens on all ip address on  port 80

reqidel ^X-Forwarded-For:.*   #  It uses the client ip address instead of letting the webserver know the request came from haproxy..  good more analysing web traffic
option forwardfor
 acl acl_mail.cyfordtechnologies.com hdr_dom(host) -i mail.cyfordtechnologies.com
 acl acl_www.cyfordtechnologies.com hdr_dom(host) -i www.cyfordtechnologies.com

 acl acl_seo.cyfordtechnologies.com hdr_dom(host) -i seo.cyfordtechnologies.com
 acl acl_Extranet.cyfordtechnologies.com hdr_dom(host) -i Extranet.cyfordtechnologies.com
 acl acl_genconinc.net hdr_dom(host) -i genconinc.net
 acl acl_www.heavensgateguild.com hdr_dom(host) -i heavensgateguild.com
 acl acl_test.cyfordtechnologies.com hdr_dom(host) -i test.cyfordtechnologies.com
 acl acl_extendeucandvraptoday.com hdr_dom(host) -i      extendeucandvraptoday.com
 acl acl_lifesbasics.net hdr_dom(host) -i      lifesbasics.net
 acl acl_lifesbasics.org hdr_dom(host) -i      lifesbasics.org
 acl acl_todayssocialissues.com hdr_dom(host) -i      todayssocialissues.com
 acl acl_sample.cyfordtechnologies.com hdr_dom(host) -i  sample.cyfordtechnologies.com
 acl acl_sample.cyfordtechnologies.com hdr_dom(host) -i  onlinemerchantshop.com
 acl acl_sample.cyfordtechnologies.com hdr_dom(host) -i  gottires.com

 use_backend www.cyfordtechnologies.com if acl_www.cyfordtechnologies.com
 use_backend mail.cyfordtechnologies.com if acl_mail.cyfordtechnologies.com
 use_backend www.cyfordtechnologies.com if acl_seo.cyfordtechnologies.com
 use_backend www.cyfordtechnologies.com if acl_Extranet.cyfordtechnologies.com
 use_backend www.cyfordtechnologies.com if acl_genconinc.net
 use_backend Hosting   if acl_www.heavensgateguild.com
 use_backend www.cyfordtechnologies.com if acl_test.cyfordtechnologies.com
 use_backend Hosting if acl_extendeucandvraptoday.com
 use_backend Hosting if acl_lifesbasics.net
 use_backend Hosting if acl_lifesbasics.org
 use_backend Hosting if acl_todayssocialissues.com
 use_backend www.cyfordtechnologies.com if acl_sample.cyfordtechnologies.com

#———————————————————————
# round robin balancing between the various backends
#———————————————————————

# I use mynmain websites in the first pool and usually seperate clients and others  incase they move around or mine move around.  i also load balanced a couple of reverse proxy servers. which uses port 3128

# Of course you would need to change the ip address  to the ip of your servers

backend www.cyfordtechnologies.com
balance roundrobin
 cookie SRVID insert indirect nocache
    server 01-CachServer_www.cyfordtechnologies.com-3128 192.168.22.8:3128  weight 10 maxconn 200 check inter 10500
    server 00-CachServer_www.cyfordtechnologies.com-3128 192.168.22.7:3128   weight 10 maxconn 200 check inter 10500
    server 01-CachServer_www.cyfordtechnologies.com-80 192.168.22.8:80   weight 17 maxconn 200 check inter 10500
    server 00-CachServer_www.cyfordtechnologies.com-80 192.168.22.7:80   weight 17 maxconn 200 check inter 10500
    server web00_www.cyfordtechnologies.com 192.168.22.150:80 cookie SRVID  weight 15 maxconn  100 check inter 15000
    server web01_www.cyfordtechnologies.com 192.168.22.151:80 cookie SRVID  weight 15 maxconn  100 check inter 15000
    server web02_www.cyfordtechnologies.com 192.168.22.152:80 cookie SRVID  weight 50 maxconn  100 check inter 15000  
    #server HostMonster_www.cyfordtechnologies.com 74.220.22.77:80 cookie SRVID  weight 5 maxconn 5 check backup inter 15000
option redispatch # send back to dispatch in case of connection failure
#option abortonclose
option forceclose

backend www.gottires.com
balance roundrobin
 cookie SRVID insert indirect nocache
    server www.gottires.com 192.168.111.32:80 cookie SRVID  weight 15 maxconn  100 check inter 15000  
    #server HostMonster_www.cyfordtechnologies.com 74.220.215.77:80 cookie SRVID  weight 5 maxconn 5 check backup inter 15000
option redispatch # send back to dispatch in case of connection failure
#option abortonclose
option forceclose

backend Hosting
balance roundrobin
 cookie SRVID insert indirect nocache
    server web00_www.cyfordtechnologies.com 192.168.44.150:80 cookie SRVID  weight 15 maxconn  100 check inter 15000
    server web01_www.cyfordtechnologies.com 192.168.44.151:80 cookie SRVID  weight 15 maxconn  100 check inter 15000
    server web02_www.cyfordtechnologies.com 192.168.44.152:80 cookie SRVID  weight 50 maxconn  100 check inter 15000
    #server HostMonster_www.cyfordtechnologies.com 74.220.215.77:80 cookie SRVID  weight 5 maxconn 5 check backup inter 15000
option redispatch # send back to dispatch in case of connection failure
#option abortonclose
option forceclose

backend mail.cyfordtechnologies.com
 server srv_mail.cyfordtechnologies.com 192.168.22.110:80 weight 10 maxconn  100  inter 20000
option abortonclose

backend www.heavensgateguild.com
 server 00-CachServer_www.cyfordtechnologies.com-3128 192.168.33.7:3128  weight 10 maxconn 200 check inter 10500
 server 01-CachServer_www.cyfordtechnologies.com-3128 192.168.33.8:3128  weight 10 maxconn 200 check inter 10500
 server web_www.cyfordtechnologies.com 192.168.33.150:80 cookie SRVID  weight 9 maxconn  100 check backup inter 15000
option abortonclose
option redispatch

Start Haproxy and make it start with system boot

# service haproxy start ; chkconfig haproxy on


Published on: Sep 14, 2020

Categories: Linux Server's