Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Varnish set up in front of Apache 2 on Ubuntu 11.10. I'm using this VCL file:

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

sub vcl_recv {

  if (req.url ~ "^/web") {
    unset req.http.cookie;
  }

  if (req.url ~ "administration" ||
    req.url ~ "preview"
  ) {
    return(pass);
  }

  if (req.http.Cookie) {
    set req.http.Cookie = ";"+req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(mag_header)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
      remove req.http.Cookie;
    }
  }

  if (req.url ~ "\.(html|gif|jpg|jpeg|png|js|css)$") {
    unset req.http.cookie;
    return(lookup);
  }

  if (req.http.x-forwarded-for) {
    set req.http.X-Forwarded-For =
    req.http.X-Forwarded-For + ", " + client.ip;
  }
  else {
    set req.http.X-Forwarded-For = client.ip;
  }
  if (req.http.x-forwarded-host) {
    set req.http.X-Forwarded-Host =
    req.http.X-Forwarded-Host + ", " + server.ip;
  }
  else {
    set req.http.X-Forwarded-Host = server.ip;
  }
  return(lookup);
}

sub vcl_hash {
  hash_data(req.http.cookie);
}

sub vcl_fetch {
  set beresp.ttl =30m;
}

The value of the mag_header cookie can only have one of three values, minimizing how many variations will be cached. I am seeing heaps of misses on the home page though (about 7 per 60 seconds), when that should be cached for 30 minutes.

Any idea why its not caching?

Cheers, Mark

EDIT

Below is a request made to the home page (unfortunately I'm not sure if this is a good example of my problem or not, let me know if I should grab another one):

29 RxHeader     c User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
29 RxHeader     c Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
29 RxHeader     c Referer: http://www.vmagazine.com/site/content/451/the-discreet-charm-of-kate-upton
29 RxHeader     c Accept-Encoding: gzip,deflate,sdch
29 RxHeader     c Accept-Language: en-US,en;q=0.8
29 RxHeader     c Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
29 RxHeader     c Cookie: vmag_header=vmag; __utma=232335640.704196202.1357394712.1357394712.1357394712.1; __utmb=232335640.25.10.1357394712; __utmc=232335640; __utmz=232335640.1357394712.1.1.utmcsr=popwatch.ew.com|utmccn=(referral)|utmcmd=referral|utmcct=/2013/01/04/krist
29 VCL_call     c recv lookup
29 VCL_call     c hash
29 Hash         c vmag_header=vmag
29 Hash         c /
29 Hash         c www.vmagazine.com
29 VCL_return   c hash
29 HitPass      c 1682448674
29 VCL_call     c pass pass
29 Backend      c 19 default default
29 TTL          c 1682448965 RFC 120 -1 -1 1357394808 0 1357394807 0 0
29 VCL_call     c fetch
29 TTL          c 1682448965 VCL 1800 -1 -1 1357394808 -0
29 TTL          c 1682448965 VCL 120 -1 -1 1357394808 -0
29 VCL_return   c hit_for_pass
29 ObjProtocol  c HTTP/1.1
29 ObjResponse  c OK
29 ObjHeader    c Date: Sat, 05 Jan 2013 14:06:47 GMT
29 ObjHeader    c Server: Apache/2.2.20 (Ubuntu)
29 ObjHeader    c X-Powered-By: PHP/5.3.6-13ubuntu3.9
29 ObjHeader    c Set-Cookie: vmag_header=vmag; path=/
29 ObjHeader    c Vary: Accept-Encoding
29 ObjHeader    c Content-Encoding: gzip
29 ObjHeader    c Content-Length: 16315
29 ObjHeader    c Content-Type: text/html
29 ObjHeader    c X-Pad: avoid browser bug
29 Gzip         c u F - 16315 77445 80 80 130452
29 VCL_call     c deliver deliver
29 TxProtocol   c HTTP/1.1
29 TxStatus     c 200
29 TxResponse   c OK
29 TxHeader     c Server: Apache/2.2.20 (Ubuntu)
29 TxHeader     c X-Powered-By: PHP/5.3.6-13ubuntu3.9
29 TxHeader     c Set-Cookie: vmag_header=vmag; path=/
29 TxHeader     c Vary: Accept-Encoding
29 TxHeader     c Content-Encoding: gzip
29 TxHeader     c Content-Type: text/html
29 TxHeader     c X-Pad: avoid browser bug
29 TxHeader     c Content-Length: 16315
29 TxHeader     c Accept-Ranges: bytes
29 TxHeader     c Date: Sat, 05 Jan 2013 14:06:47 GMT
29 TxHeader     c X-Varnish: 1682448965
29 TxHeader     c Age: 0
29 TxHeader     c Via: 1.1 varnish
29 TxHeader     c Connection: keep-alive
29 Length       c 16315
29 ReqEnd       c 1682448965 1357394807.567663431 1357394807.963178158 0.000065088 0.395375967 0.000138760
share|improve this question
    
Are you seeing any cache hits? There could be other values in the cookie which could be keeping it from caching much but still caching a little. If there are no hits at all, that would confirm an issue with your configuration. –  pjmorse Jan 4 '13 at 16:50
    
Could you tell me how to read current hits? I am using this command to show misses: varnishtop -b -i TxURL –  Mark Davies Jan 4 '13 at 16:51
    
I am getting some hits. A basic varnishstat shows 2.09 cache_hit 1.03 cache_hitpass 0.67 cache_miss –  Mark Davies Jan 4 '13 at 17:04
    
What is the URL of the home page ? –  GeekRide Jan 4 '13 at 20:26
1  
It looks like I'm getting "hit for pass" on the home page maybe? varnishstat shows: 1.94 cache_hit 0.64 cache_hitpass 0.57 cache_miss –  Mark Davies Jan 5 '13 at 14:25
show 2 more comments

1 Answer

up vote 0 down vote accepted

The problem was that there was a session_start() call in my code, which added a PHP_SESSION cookie, meaning the cache was never hitting.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.