Reference
enable 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.
haproxybackend serverscookie SERVER_USED insert indirect nocacheserver web1 192.168.0.10:80 check cookie web1server web2 192.168.0.11:80 check cookie web2
haproxybackend serverscookie SERVER_USED insert indirect nocacheserver web1 192.168.0.10:80 check cookie web1server 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.
haproxybackend serverscookie SERVER_USED insert indirect nocache dynamicdynamic-cookie-key mysecretphraseserver web1 192.168.0.10:80 checkserver web2 192.168.0.11:80 check
haproxybackend serverscookie SERVER_USED insert indirect nocache dynamicdynamic-cookie-key mysecretphraseserver web1 192.168.0.10:80 checkserver 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.
haproxyresolvers mydnsnameserver dns1 192.168.50.30:53accepted_payload_size 8192backend serverscookie SERVER_USED insert indirect nocache dynamicdynamic-cookie-key mysecretphraseserver-template web 5 myservice.example.local:80 check resolvers mydns init-addr libc,none
haproxyresolvers mydnsnameserver dns1 192.168.50.30:53accepted_payload_size 8192backend serverscookie SERVER_USED insert indirect nocache dynamicdynamic-cookie-key mysecretphraseserver-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 enable dynamic-cookie backend command to enable session persistence that was previously disabled with disable dynamic-cookie backend.
Examples Jump to heading
If session persistence has been disabled, enable it again by calling enable dynamic-cookie backend with the name of the backend.
nixecho "enable dynamic-cookie backend servers" | \sudo socat stdio tcp4-connect:127.0.0.1:9999
nixecho "enable 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?