Master HAProxy: Configure ACLs, Backends, Frontends, and Stats in Minutes
This guide explains how to set up HAProxy, covering core concepts such as ACLs, backends, frontends, optional statistics, a complete configuration example, and step‑by‑step deployment on Linux, enabling you to quickly build a reliable load‑balancing proxy.
HAProxy (High Availability Proxy) is a free, fast, and reliable load‑balancing and proxy solution written in C for TCP/HTTP‑based applications.
Basic Concepts
The main components of an HAProxy configuration are Access Control Lists (ACLs), backends, and frontends.
ACL
An ACL is a test expression that decides actions based on the result of the test, such as selecting a server and forwarding a request.
Syntax:
acl <aclname> <criterion> [flags] [operator] [<value>] ...Example:
acl acl_myApp path_sub myAppBackend
A backend is a group of servers that actually process forwarded requests. It includes a load‑balancing algorithm and a list of servers with ports.
Basic syntax:
backend <backendname>
balance <loadbalancing algorithm>
server <name> <ip>:<port> check
...Example:
backend myAppBackend
balance roundrobin
server myAppServer1 172.21.28.1:8080 check
server myAppServer2 172.21.28.2:8080 checkFrontend
The frontend defines how requests are forwarded to a backend. It consists of IP addresses, a port, ACLs, and use_backend rules.
Syntax:
frontend <frontend name>
bind <IPs or wildcard>:80
acl <aclname> <criterion> [flags] [operator] [<value>] ...
use_backend <backend name> if <aclname>Example:
frontend myAppFrontEnd
bind *:80
acl acl_myApp path_sub myApp
use_backend myAppBackend if acl_myAppStatistics (Optional)
HAProxy can expose a statistics page showing server status, connection counts, etc. Enable it by adding a listen stats section:
listen stats
bind *:9999
stats enable
stats hide-version
stats uri /stats
stats auth admin:admin@123Complete Configuration Example (haproxy.cfg)
#HA Proxy Config
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen stats
bind *:9999
stats enable
stats hide-version
stats uri /stats
stats auth admin:admin@123
frontend myApp
bind *:80
acl acl_myApp path_sub myApp
use_backend myAppBackEnd if acl_myApp
backend myAppBackEnd
balance roundrobin
server myAppServer1 127.0.0.1:8081 check
server myAppServer2 127.0.0.1:8082 checkDeploy HAProxy on Linux
Download the source code from the HAProxy website.
Extract it: tar xvzf haproxy-1.8-dev1.tar.gz Compile: cd haproxy-1.8-dev1 && make TARGET=linux2628 Create haproxy.cfg with the configuration above.
Start HAProxy: ./haproxy -f haproxy.cfg Access the stats page at http://localhost:9999/stats.
When accessed, the stats page displays the status interface as shown in the image below.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
