How to setup varnish to do not cache particular url

In this example I am sharing about my own VPS which is hosted in DigitalOcean. In my last post I already written about how to setup varnish to do not cache specific url but it was the case of single URL.

Now in this example we have address like http://example.come and http://www.example.com . In given below sample from default.vcl file,here I used the pipe (|) symbol. If you have any other subdomain like static.example.com, then in line no. 2 from below given sample, use it like this “(static.example.com|www.example.com|example.com)”

Note: Below dotted lines (….) represents other texts written above this sample configuration. So kindly ignore

Find the sub vcl_recv section in /etc/varnish/defualt.vcl file and paste the given below code (from line no. 10 to 12 of given below sample) just before the sub vcl_recv block ended with line return (lookup)

vi /etc/varnish/defualt.vcl

sub vcl_recv {

......
......
......

   # do not cache example.com
   if (req.http.host ~ "(www.example.com|example.com)") {
     return(pass);
   }

    return (lookup);
}

Restart the varnish

After editing,restart the varnish


/etc/init.d/varnish restart

How to check

To check the given practical is working or not. Use command “curl -I http://url-of-site”

Below given is reference of my another personal blog (Dated: Oct-09-2013)

linux@tuxworld:/tmp$ curl -I sharad.me
HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Wed, 09 Oct 2013 16:26:15 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Set-Cookie: __cfduid=ddc1f0ed340412dc378042ea6a1315a4d1381335974307; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.sharad.me
X-Pingback: http://sharad.me/xmlrpc.php
Accept-Ranges: bytes
X-Varnish: 1714479446
Age: 0
Via: 1.1 varnish
CF-RAY: bac6e6f6c470388

linux@tuxworld:/tmp$ curl -I www.sharad.me
HTTP/1.1 301 Moved Permanently
Server: cloudflare-nginx
Date: Wed, 09 Oct 2013 16:27:23 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Set-Cookie: __cfduid=db88b51d4cc9ec9b4c2958ec7a1002f071381336043340; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.sharad.me
X-Pingback: http://sharad.me/xmlrpc.php
Location: http://sharad.me/
Accept-Ranges: bytes
X-Varnish: 1714479449
Age: 0
Via: 1.1 varnish
CF-RAY: bac701ee1b80388

linux@tuxworld:/tmp$ 

2 thoughts on “How to setup varnish to do not cache particular url”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.