HAProxy/Varnish: redirecting a percentage of traffic

I'm trying to measure separate statistics for two different versions of a web page. So I need to redirect a certain percentage of my web traffic to a different page than the one requested (i.e. 20% of requests for page_1 go to page_1.1).
It doesn't seem to me that Varnish can natively count traffic or requests.
I'm thinking that I could have the Apache backend count the requests, insert a custom header and then have Varnish send a restart to the client if the backend response contains that certain header.

Any ideas or suggestions?

Cheers,
Jeremy

EDIT: I forgot to mention that there is an HAProxy istance in front of Varnish, so I was thinking that another good way to do this could be:

On HAProxy:
count requests for page1
if count > 80 insert custom header
if count = 100 reset counter

On Varnish
if custom header present in request issue client restart with page1—>page1.1 rewrite

Not sure how to do this with ACL, gpc0 and stick-tables. I'm studying to find a solution :-)

As always, ideas or suggestions are more than welcome


Solution 1:

I've never had to configure a Varnish server but I imagine this is possible using the built in load balancer with a round-robin configuration.

For example, if you configured 4 backend pointing at the old version of your site/page and 1 at the new version, like this..

backend old1 {
  .host = "old.example.com";
  .probe = { .url = "/"; .interval = 5s; .timeout = 1 s; .window = 5; .threshold = 3; }
}
backend old2 {
  .host = "old.example.com";
  .probe = { .url = "/"; .interval = 5s; .timeout = 1 s; .window = 5; .threshold = 3; }
}
backend old3 {
  .host = "old.example.com";
  .probe = { .url = "/"; .interval = 5s; .timeout = 1 s; .window = 5; .threshold = 3; }
}
backend old4 {
  .host = "old.example.com";
  .probe = { .url = "/"; .interval = 5s; .timeout = 1 s; .window = 5; .threshold = 3; }
}
backend new {
  .host = "new.example.com";
  .probe = { .url = "/"; .interval = 5s; .timeout = 1 s; .window = 5; .threshold = 3; }
}

And then had a director that round-robin'ed between them...

director blah round-robin {
  { .backend = old1; }
  { .backend = old2; }
  { .backend = old3; }
  { .backend = old4; }
  { .backend = new; }
}

The new.example.com version of the site would get 20% of the traffic.

This is a bit of a hack (there may well be a better solution) but I believe this would work.