Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Created May 16, 2022 16:25
Show Gist options
  • Save haproxytechblog/11b14d15be71368026b2ae1930b81333 to your computer and use it in GitHub Desktop.
Save haproxytechblog/11b14d15be71368026b2ae1930b81333 to your computer and use it in GitHub Desktop.
Enable Sticky Sessions in HAProxy
frontend mywebapp
bind :80
mode http
default_backend webservers
backend webservers
mode http
balance roundrobin
cookie SERVER insert indirect nocache
server web1 192.168.56.20:80 check cookie web1
server web2 192.168.56.21:80 check cookie web2
frontend mywebapp
bind :80
mode http
default_backend webservers
resolvers mydns
nameserver dns1 192.168.56.30:53
accepted_payload_size 8192
backend webservers
mode http
balance roundrobin
cookie SERVER insert indirect nocache dynamic
dynamic-cookie-key mysecretphrase
server-template web 10 myservice.example.local:80 check resolvers mydns init-addr libc,none
frontend mywebapp
bind :80
mode http
default_backend webservers
peers sticktables
bind :10000
# On the next line, 'loadbalancer1'
# is the HAProxy server's hostname
server loadbalancer1
table sticky-sessions type ip size 1m
backend webservers
mode http
balance roundrobin
stick match src table sticktables/sticky-sessions
stick store-request src table sticktables/sticky-sessions
server web1 192.168.56.20:80 check
server web2 192.168.56.21:80 check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment