How to set Varnish so that it doesn't cache a specific URL with a query string?

8,107

Putting the following block in vcl_recv should do it:

if ( req.url ~ "^/\?random" ) {
  return (pass);
}

When you return(pass), you will jump past the opportunity to look up the request in the cache.

The default behavior is to do a bunch of tests to see if it is likely that the content is dynamic (does it set cookies, does it require authentication, is it a POST-request, things like that) - if none of those conditions are met, Varnish falls back to return(lookup)

Share:
8,107

Related videos on Youtube

Kevin Worthington
Author by

Kevin Worthington

Professional Software Engineer, Systems Administrator, Applications Specialist, Internet Enthusiast, and Website Builder.

Updated on September 18, 2022

Comments

  • Kevin Worthington
    Kevin Worthington over 1 year

    Server setup: Ubuntu 12.10, Varnish 3.0.2, Nginx 1.3.14, with a WordPress 3.5.1 install.

    We are using the "Random Redirect" plugin which shows a random blog post at the URL http://example.com/?random

    I would like to set Varnish to not cache the above URL, since currently the "random" post keeps showing the same post.

    A specific snippet to show what to put in default.vcl would be super helpful. Thanks.

  • Kevin Worthington
    Kevin Worthington about 11 years
    I tried almost the exact code before posting my question and it didn't work. I tried your code and it also didn't work. I got the following errors: Command failed with error code 106 Message from VCC-compiler: Syntax error at ('input' Line 48 Pos 16) if ( req.url ~ '^/?random' ) { ---------------#-------------- Running VCC-compiler failed, exit 1 VCL compilation failed
  • Ziron5
    Ziron5 about 11 years
    I'm sorry, I misquoted -- one should use "" and not '' ... It compiles in my test.
  • Kevin Worthington
    Kevin Worthington about 11 years
    The double quotes do compile correctly, but the random link stay the same for me still.
  • Ziron5
    Ziron5 about 11 years
    I forgot to add escaping of the ?, since it was interpreted as a part of the regular expression. Again, sorry about that.
  • Kevin Worthington
    Kevin Worthington about 11 years
    Yes, I think that fixed it up. Thanks Kvisle. (Giving you the check mark and voting up.)