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.

In vcl_recv I decide whether to pass or lookup based on the existence of a cookie:

sub vcl_recv {
    if (req.http.Cookie ~ "(JSESSIONID=)" ) {
            /* do not cache logged in users */
            return (pass);
    }
    return (lookup);
}

In vcl_fetch I fine tune caching for some pages:

sub vcl_fetch {

    /* custom rules block */
    if (req.url ~ "^/foo") { set beresp.ttl=30s; }
    if (req.url ~ "^/bar") { set beresp.ttl=1m; }
    if (req.url ~ "^/123") { set beresp.ttl=10m; }

    return (deliver);
}

How can I know within vcl_fetch whether I am in pass or lookup mode? I'd like to be able to avoid running the "custom rules block" if I am in lookup mode because the logged in users will anyway run in pass mode so these rules do not apply to them.

share|improve this question
add comment

1 Answer

You can always set a custom header in vcl_recv:

sub vcl_recv {
  if (req.http.Cookie ~ "(JSESSIONID=)" ) {
    /* do not cache logged in users */
    set req.http.x-my-mark = "passed";
    return (pass);
  }
  return (lookup);
}
#...
sub vcl_fetch {
  if (req.http.x-my-mark == "passed") {
    /* custom rules block */
    if (req.url ~ "^/foo") { set beresp.ttl=30s; }
    if (req.url ~ "^/bar") { set beresp.ttl=1m; }
    if (req.url ~ "^/123") { set beresp.ttl=10m; }
  }

  return (deliver);
}

But I think it will be much cleaner to put your login on vcl_pass[1] or just perform your original check on vcl_fetch:

sub vcl_fetch {
  if (req.http.Cookie ~ "(JSESSIONID=)") {
    /* custom rules block */
    if (req.url ~ "^/foo") { set beresp.ttl=30s; }
    if (req.url ~ "^/bar") { set beresp.ttl=1m; }
    if (req.url ~ "^/123") { set beresp.ttl=10m; }
  }

  return (deliver);
}

[1] https://www.varnish-software.com/static/book/VCL_functions.html#vcl-vcl-pass

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.