enable dynamic-cookie backend
Enable session persistence when dynamic cookies are in use.
To send a client to the same server where they were sent previously in order to reuse a session on that server, you can enable cookie-based session persistence. Add a
cookie directive to the
backend section and set the
cookie parameter to a unique value on each
Below, we've enabled cookie-based session persistence, which means that HAProxy Enterprise places an HTTP cookie that contains the server's unique cookie value (web1 or web2) into the client's browser. The client attaches that cookie to each of its subsequent requests so that the load balancer knows which server to use. For example, if a client was sent to the server web1 first, it will be sent to that same server from then on.
backend servers cookie
SERVER_USEDinsert indirect nocache server web1 192.168.0.10:80 check cookie web1 server web2 192.168.0.11:80 check cookie web2
Instead of setting hardcoded cookie values, you can have HAProxy Enterprise generate them automatically. Add the dynamic parameter to the
cookie line in order to generate a value for the SERVER_USED cookie that's based on the server's IP address, port, and a secret key that's specified with
dynamic-cookie-key. Note that we no longer specify the
cookie parameter on the
backend servers cookie
SERVER_USEDinsert indirect nocache dynamic dynamic-cookie-key mysecretphrase server web1 192.168.0.10:80 check server web2 192.168.0.11:80 check
This is especially useful when servers are added to the backend dynamically, since it can be difficult to set the cookie value ahead of time. In the snippet below, we are using
server-template to populate server lines dynamically based on DNS service discovery information. Therefore, we let the load balancer generate the cookie values.
resolvers mydns nameserver
dns1192.168.50.30:53 accepted_payload_size 8192 backend servers cookie SERVER_USEDinsert indirect nocache dynamic dynamic-cookie-key mysecretphrase server-template web5 myservice.example.local:80 check resolvers mydns init-addr libc,none
When using dynamic cookie values, you can use the Runtime API's
enable dynamic-cookie backend command to enable session persistence that was previously disabled with
disable dynamic-cookie backend.
If session persistence has been disabled, enable it again by calling
enable dynamic-cookie backend with the name of the backend.
$ echo "enable dynamic-cookie backend servers" | sudo socat /var/run/hapee-2.3/hapee-lb.sock stdio
Next upenable frontend