title: Apache load balancer with cookies, change ROUTEID / sticky cookie in PHP
body: <p>I have 2 servers that I'm load balancing traffic with using apache load balancer. This serve's two purposes, load balancing and reverse proxy so that all hosts have the same URL.
  This setup is working perfectly using cookies to for stickiness.</p>
  
  <p>In some cases I may need to 'force' the balancer to use a specific host (BalanceMember). This is an absolute requirement and the reason is beyond the scope of this discussion.</p>
  
  <p>I have this working using PHP by simply changing the ROUTEID cookie from 1 to 2 for example. 
  My first question is, why do I have to destroy the PHP session after I've changed the COOKIE value in order for it to take effect. The ROUTEID does not change if I don't destroy the session after changing it.</p>
  
  <p>My second question is, is this the best way to achieve this goal, bearing in mind that it does work as desired.</p>
  
  <p>Apache proxy.conf</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
  Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; Expires=-1 path=/" env=BALANCER_ROUTE_CHANGED
  
  &lt;Proxy "balancer://mycluster"&gt;
      BalancerMember http://myhost1.example.com/ route=1
      BalancerMember http://myhost2.example.com/ route=2
  
      ProxySet lbmethod=byrequests failontimeout=on
  &lt;/Proxy&gt;
  
  ProxyPreserveHost Off
  
  RemoteIPHeader X-Forwarded-For
  RemoteIPInternalProxy 127.0.0.0/8
  
  # Enable SSL Proxying
  ProxyRequests Off
  SSLProxyEngine on
  
  ProxyPass / balancer://mycluster/ stickysession=ROUTEID
  ProxyPassReverse / balancer://mycluster/
  
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>PHP Code to change ROUTEID</p>
  
  <pre><code>session_name("my_example_session");
  session_start();
  
  
  ob_start();
  $cookie_name = "ROUTEID";
  $cookie_value = "balancer.2";
  setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30)); // 86400 = 1 day
  ob_flush();
  
  session_destroy();
  session_write_close();
  </code></pre>