Reference

disable dynamic-cookie backend

Enable session persistence when dynamic cookies are in use.

Description Jump to heading

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 server line.

Below, we’ve enabled cookie-based session persistence, which means that the load balancer 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.

haproxy
backend servers
cookie SERVER_USED insert indirect nocache
server web1 192.168.0.10:80 check cookie web1
server web2 192.168.0.11:80 check cookie web2
haproxy
backend servers
cookie SERVER_USED insert 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 the load balancer 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 server lines.

haproxy
backend servers
cookie SERVER_USED insert indirect nocache dynamic
dynamic-cookie-key mysecretphrase
server web1 192.168.0.10:80 check
server web2 192.168.0.11:80 check
haproxy
backend servers
cookie SERVER_USED insert 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.

haproxy
resolvers mydns
nameserver dns1 192.168.50.30:53
accepted_payload_size 8192
backend servers
cookie SERVER_USED insert indirect nocache dynamic
dynamic-cookie-key mysecretphrase
server-template web 5 myservice.example.local:80 check resolvers mydns init-addr libc,none
haproxy
resolvers mydns
nameserver dns1 192.168.50.30:53
accepted_payload_size 8192
backend servers
cookie SERVER_USED insert indirect nocache dynamic
dynamic-cookie-key mysecretphrase
server-template web 5 myservice.example.local:80 check resolvers mydns init-addr libc,none

When using dynamic cookie values, you can use the Runtime API’s disable dynamic-cookie backend command to disable session persistence for a backend.

Examples Jump to heading

Disable session persistence by calling disable dynamic-cookie backend with the name of the backend.

nix
echo "disable dynamic-cookie backend servers" | \
sudo socat stdio tcp4-connect:127.0.0.1:9999
nix
echo "disable dynamic-cookie backend servers" | \
sudo socat stdio tcp4-connect:127.0.0.1:9999

See also Jump to heading

Do you have any suggestions on how we can improve the content of this page?