Step-by-Step HAProxy Deployment and Configuration Guide for Load Balancing
This tutorial walks through installing HAProxy, configuring global, defaults, listen, frontend, and backend sections, setting up ACL‑based routing, defining server groups for default, website, and blog services, testing load‑balancing behavior, and accessing the HAProxy statistics page, all with complete command‑line examples.
HAProxy Deployment Configuration
Topology diagram
Explanation
haproxy server IP: 172.16.253.200/16 (external), 192.168.29.140/24 (internal)
Blog server group IP: 192.168.29.130/24, 192.168.29.131/24
Website server group IP: 192.168.29.120/24, 192.168.29.121/24
Default server group IP: 192.168.29.110/24, 192.168.29.111/24HAProxy Host Configuration
# yum install haproxy -y
# vim /etc/haproxy/haproxy.cfg1. Global Section
Sets global process‑level parameters, often related to OS configuration.
global
log 127.0.0.1 local2 info ## global log config
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid ## PID file
maxconn 4000 ## max concurrent connections
user haproxy ## run as user
group haproxy ## run as group
daemon ## run in background
stats socket /var/lib/haproxy/stats2. Defaults Section
Defines default parameters that are inherited by frontend, backend, and listen sections unless overridden.
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 5s
timeout client 10s
timeout server 10s
timeout http-keep-alive 10s
timeout check 2s
maxconn 30003. Listen Section
Combines frontend and backend functionality for the admin statistics page.
listen admin_stats
bind 0.0.0.0:19088 ## stats page IP and port
mode http
log 127.0.0.1 local2 err
stats refresh 30s ## auto‑refresh interval
stats uri /haproxy-status ## URL path for stats page
stats realm welcome login Haproxy
stats auth admin:admin ## credentials
stats hide-version
stats admin if TRUE4. Frontend Section
Defines the front‑end virtual node that receives client requests and selects a backend via ACL rules.
frontend www
bind *:80
mode http
option httplog
option forwardfor
option httpclose
log globalACL rules enable intelligent load balancing:
acl host_www hdr_reg(host) -i ^(www.tb.com|tb.com)
acl host_blog hdr_beg(host) -i blog.
use_backend server_www if host_www
use_backend server_blog if host_blog
default_backend server_default5. Backend Sections
Configure backend server pools for default, www, and blog services.
backend server_default
mode http
option redispatch
option abortonclose
balance roundrobin
cookie SERVERID
option httpchk GET /check_status.html
server default1 192.168.29.110:80 cookie default1 weight 2 check inter 2000 rise 2 fall 3
server default2 192.168.29.111:80 cookie default2 weight 2 check inter 2000 rise 2 fall 3
backend server_www
mode http
option redispatch
option abortonclose
balance source
cookie SERVERID
option httpchk GET /check_status.jsp
server www1 192.168.29.120:80 cookie www1 weight 6 check inter 2000 rise 2 fall 3
server www2 192.168.29.121:80 cookie www2 weight 6 check inter 2000 rise 2 fall 3
backend server_blog
mode http
option redispatch
option abortonclose
balance roundrobin
cookie SERVERID
option httpchk GET /check_blog.php
server blog1 192.168.29.130:80 cookie blog1 weight 5 check inter 2000 rise 2 fall 3
server blog2 192.168.29.131:80 cookie blog2 weight 5 check inter 2000 rise 2 fall 3Backend Server Configuration
1. Default Server
Virtual host and status page setup for 192.168.29.110 and .111.
# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost 192.168.29.110:80>
DocumentRoot "/data/web1"
<Directory "/data/web1/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
# echo "This is check_status.page ip:192.168.29.110" > /data/web1/check_status.html
# echo "This is default page ip:192.168.29.110" > /data/web1/index.html2. Website Server
Configuration for 192.168.29.120 and .121 (www.tb.com).
# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost 192.168.29.120:80>
DocumentRoot "/data/web1"
ServerName www.tb.com
<Directory "/data/web1/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
# echo "This is check_status page ip:192.168.29.120" > /data/web1/check_status.jsp
# echo "This is www page ip:192.168.29.120" > /data/web1/index.html3. Blog Server Group
Configuration for 192.168.29.130 and .131 (www.blog.tb.com).
# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost 192.168.29.130:80>
DocumentRoot "/data/web1"
ServerName www.blog.tb.com
<Directory "/data/web1/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
# echo "This is check_status page ip:192.168.29.130" > /data/web1/check_blog.php
# echo "This is blog page ip:192.168.29.130" > /data/web1/index.htmlTesting
1. Start HAProxy
# systemctl start haproxy2. Test Website Servers
for i in {1..10}; do curl www.tb.com; done
# All responses show the same IP (192.168.29.120) due to session stickiness.3. Test Blog Servers
for i in {1..10}; do curl blog.tb.com; done
# Responses alternate between 192.168.29.130 and 192.168.29.131, confirming round‑robin load balancing.4. Test Default Servers
for i in {1..10}; do curl 172.16.253.200; done
# Responses alternate between 192.168.29.110 and 192.168.29.111.5. HAProxy Statistics Page
Access http://172.16.253.200:19088/haproxy-status, log in with the configured credentials to view real‑time statistics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
