PHP Sessions Keep Resetting Behind HAProxy: Sticky Sessions with a Cookie

Classic symptom after putting a load balancer in front of two PHP servers: users get logged out at random, a cart empties itself, a multi-step form forgets what you typed. Nothing is “broken” on either server. The problem is where PHP keeps sessions. Below is a minimal way to reproduce it, the explanation, and the fix with HAProxy’s cookie-based persistence (sticky sessions). Reproduce it in one minute Two Apache + PHP backends (apache1, apache2) behind HAProxy with balance roundrobin. On both servers, create /var/www/html/session.php: ...

December 6, 2026 · 4 min · Sysadmin Field Notes