Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Setup HAproxy Load Balance Server for Sharing Web Traffic
This article demonstrates how to configure HAProxy as a load balancer to distribute web traffic across multiple servers, providing high availability and improved performance for web applications. HAProxy is a free, open-source TCP/HTTP load balancer that ensures your website remains accessible even when individual servers fail.
Server Setup Overview
For this tutorial, we'll configure three CentOS 6.7 servers:
HAProxy Load Balancer: 192.168.57.150
Web Server 1: 192.168.57.147
Web Server 2: 192.168.57.148
Installation
Setting up Web Servers
First, install Apache HTTP server on both web servers:
# yum install httpd
Start Apache and verify it's accessible via web browser at http://server-ip-address.
Installing HAProxy
On the load balancer server, install HAProxy:
# yum install haproxy openssl-devel
Configuring Host Files
Add the following entries to /etc/hosts on all three servers:
# vi /etc/hosts 192.168.57.150 haproxy.demo.com haproxy 192.168.57.147 webserv1.demo.com webserv1 192.168.57.148 webserv2.demo.com webserv2
Enabling HAProxy Logging
Configure logging for troubleshooting by editing the rsyslog configuration:
# vi /etc/rsyslog.conf
Enable UDP syslog reception by uncommenting these lines:
$ModLoad imudp $UDPServerRun 514
Create HAProxy log configuration:
# vi /etc/rsyslog.d/haproxy.conf local0.* /var/log/haproxy.log
Restart rsyslog service:
# service rsyslog restart
HAProxy Configuration
Edit the main HAProxy configuration file:
# vi /etc/haproxy/haproxy.cfg
Add the global and defaults section:
global
log 127.0.0.1 local0
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
Configure the frontend and backend sections:
frontend web_frontend
bind 192.168.57.150:80
reqadd X-Forwarded-Proto:\ http
default_backend web_servers
backend web_servers
mode http
stats enable
stats hide-version
stats uri /stats
stats realm HAProxy\ Statistics
stats auth admin:password
balance roundrobin
option httpchk
option httpclose
option forwardfor
cookie SERVERID insert indirect nocache
server webserv1 192.168.57.147:80 cookie webserv1 check
server webserv2 192.168.57.148:80 cookie webserv2 check
Starting HAProxy Service
Start and enable HAProxy service:
# service haproxy start # chkconfig haproxy on
Verify the service status:
# chkconfig --list haproxy
Testing the Configuration
Create unique index.html files on each web server to identify which server is serving requests.
Web Server 1 (/var/www/html/index.html):
<html>
<head>
<title>HAProxy Test Page</title>
</head>
<body>
<h1>HAProxy Test - Web Server 1</h1>
</body>
</html>
Web Server 2 (/var/www/html/index.html):
<html>
<head>
<title>HAProxy Test Page</title>
</head>
<body>
<h1>HAProxy Test - Web Server 2</h1>
</body>
</html>
Load Balancing Verification
Access the HAProxy server at http://192.168.57.150. You should see content from one of the web servers. To test failover:
Stop Apache on Web Server 1:
# service httpd stop
Refresh the HAProxy URL − traffic should automatically redirect to Web Server 2.
Access the statistics page at
http://192.168.57.150/stats(use admin:password credentials) to monitor server status.
Key Configuration Parameters
| Parameter | Description |
|---|---|
| balance roundrobin | Distributes requests evenly across available servers |
| option httpchk | Enables HTTP health checks for backend servers |
| stats uri /stats | Provides web-based statistics and monitoring interface |
| cookie SERVERID | Enables session persistence using cookies |
Conclusion
HAProxy provides robust load balancing capabilities that ensure high availability and improved performance for web applications. By distributing traffic across multiple backend servers and automatically handling server failures, HAProxy maintains service continuity and enhances user experience.
