Here’s a list of frequently asked questions (FAQ) about HAProxy, which is widely used as a high-performance load balancer and reverse proxy for TCP and HTTP-based applications.
apt
(for Ubuntu/Debian) or yum
(for CentOS/RedHat):
sudo apt-get install haproxy
sudo yum install haproxy
/etc/haproxy/haproxy.cfg
and includes several key sections:
frontend https
bind *:443 ssl crt /etc/ssl/certs/your-cert.pem
default_backend servers
backend servers
server server1 192.168.1.10:80 check
backend my-backend
server server1 192.168.1.10:80 weight 5
server server2 192.168.1.11:80 weight 10
backend my-backend
server server1 192.168.1.10:80 check
maxconn
values in the global section for higher throughput.tune.bufsize
).global
section:global
log 127.0.0.1 local0
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
frontend stats
bind *:8080
stats enable
stats uri /haproxy?stats
stats auth admin:password
frontend http
log global
option httplog
tcpdump
or other packet capture tools to debug traffic in and out of HAProxy.frontend http
stick-table type ip size 1m expire 30s store conn_rate(3s)
tcp-request connection reject if { src_conn_rate(3s) gt 10 }
frontend http
timeout client 30s
backend servers
timeout server 30s
Sticky sessions, also known as session affinity or persistence, is a feature of HAProxy that ensures that all requests from a particular client are routed to the same server in a cluster.
Sticky sessions are useful for applications that require session state to be maintained across multiple requests, such as e-commerce applications, online gaming, and social networking websites. With sticky sessions, the load balancer identifies the client by using information such as the client’s IP address or a session ID, and then routes all subsequent requests from that client to the same server. This ensures that session data is not lost and that the user experience remains consistent.
To implement sticky sessions in HAProxy, you can use a combination of the “cookie” and “source” load balancing algorithms. The “cookie” algorithm assigns a cookie to each client, which is used to identify the client and route subsequent requests. The “source” algorithm uses the client’s IP address to route requests to the same server.
Overall, sticky sessions are a useful feature in HAProxy that can improve the performance and reliability of your application by ensuring that session state is maintained and consistent across multiple requests.
backend servers
cookie SERVERID insert
server server1 192.168.1.10:80 cookie s1
server server2 192.168.1.11:80 cookie s2
frontend ws
bind *:80
option http-server-close
timeout client 30s
backend ws-backend
timeout server 30s
server ws-server1 192.168.1.20:8080
frontend http
acl is_api path_beg /api
use_backend api_backend if is_api
graceful reload
feature, which allows existing connections to be maintained during a reload:sudo systemctl reload haproxy