3

I'm currently working on a Varnish (v3) set up, but I was wondering if anyone had any advice on the following.

I'm trying to use PHP's Header() function to set a custom header, for example, Test: CustomHeader. The ultimate aim is to allow Varnish to serve cached pages UNLESS a browser, in this case, has added something to a shopping basket. I was thinking that in my VCL config file, I can do something like:

if (bereq.http.Test ~ "CustomHeader") {
    set beresp.ttl = 0s;
    return (hit_for_pass);
}

However, this is always evaluate as false\no match. If I set it to

if (bereq.http.Test != "CustomHeader") {
    set beresp.ttl = 0s;
    return (hit_for_pass);
}

Then hit_for_pass is always triggered, which leads me to believe there's something amiss with my header setting. I thought it might be because the PHP header won't be set due to the whole point of the cache, but examining the headers in varnishlog DOES show the customer header appearing though, which is confusing! Additionally, accessing the page in question always performs a cache hit. Does anyone have any tips\advice on this kind of header manipulation in Varnish? Many thanks -B

flukeflume
  • 707
  • 1
  • 6
  • 14

1 Answers1

2

Using custom headers for something like this is a bad idea. Many proxies will strip headers they don't recognise, so you would not be able to rely on this even if you get it working the way you want it to.

This is what cookies were designed for - use them instead.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Many thanks for this! I tried using cookies but couldn't get it to work, but I think I'll just have to try harder :) Thanks again – flukeflume Jan 19 '12 at 11:39
  • @DaveRandom - Your position seems the opposite to what people say in http://stackoverflow.com/questions/1810915 - could you elaborate and give an example of a proxy stripping custom headers? Thanks! – Jan Żankowski Nov 04 '16 at 15:07