How to set Varnish so that it doesn't cache a specific URL with a query string?
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)
Related videos on Youtube
Kevin Worthington
Professional Software Engineer, Systems Administrator, Applications Specialist, Internet Enthusiast, and Website Builder.
Updated on September 18, 2022Comments
-
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 about 11 yearsI 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 about 11 yearsI'm sorry, I misquoted -- one should use
""
and not''
... It compiles in my test. -
Kevin Worthington about 11 yearsThe double quotes do compile correctly, but the random link stay the same for me still.
-
Ziron5 about 11 yearsI forgot to add escaping of the ?, since it was interpreted as a part of the regular expression. Again, sorry about that.
-
Kevin Worthington about 11 yearsYes, I think that fixed it up. Thanks Kvisle. (Giving you the check mark and voting up.)