How to setup Varnish to do not cache particular url specific domain

This is again with my post I come back to share how I setup a Varnish in sharadchhetri.com network which is hosted in DigitalOcean but this time scenario is different, we do not want a particular URL or domain name should be cached by Varnish.
In this real scenario I have taken our own URL called myip.sharadchhetri.com (with this link you can get information about what is your ip address and your GeoMap location of IP address[ISP point]).

Paste the given below lines in sub vcl_recv section and be careful also it must be pasted inside return(lookup) of sub vcl_recv. Just below given section ,I have shared my default.vcl file.Check from line no. 43 to 46.
Note: replace myip.sharadchhetri.com to your URL.

Edit /etc/varnish/defualt.vcl file and paste the below given line

  # dont cache myip.sharadchhetri.com
   if (req.http.host ~ "(myip.sharadchhetri.com)") {
     return(pass);
   }

After pasting the code in default.vcl,restart the varnish

/etc/init.d/varnish restart

How to check,is configuration working or not:

Use the below command from your system(curl must be installed)

curl -I http://your-domain-name.com

The below given is reference from my server

[root@myserver ~]# cat /etc/varnish/default.vcl
backend default {
  .host = "127.0.0.1";
  .port = "8880";
}
##################################################
sub vcl_recv {
    if (req.restarts == 0) {
        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.request == "PURGE") {
        if ( client.ip != "192.241.190.251") {
            error 405 "Not allowed.";
        }
        return (lookup);
    }
    if (req.request != "GET" &&
        req.request != "HEAD" &&
        req.request != "PUT" && 
        req.request != "POST" &&
        req.request != "TRACE" &&
        req.request != "OPTIONS" &&
        req.request != "DELETE") {
            return (pipe);
    }
    if (req.request != "GET" && req.request != "HEAD") {
        return (pass);
    }
    if (!(req.url ~ "wp-(login|admin)") &&
        !(req.url ~ "&preview=true" ) ) {
        unset req.http.cookie;
    }
 
    if (req.http.Authorization || req.http.Cookie) {
        return (pass);
    }

    # dont cache myip.sharadchhetri.com
   if (req.http.host ~ "(myip.sharadchhetri.com)") {
     return(pass);
   }

    return (lookup);
}

sub vcl_fetch {
    if (!(req.url ~ "wp-(login|admin)")) {
        unset beresp.http.set-cookie;
        set beresp.ttl = 96h;
    }
 
    if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
            set beresp.ttl = 120 s;
            return (hit_for_pass);
    }
    return (deliver);
}
 
sub vcl_hit {
    if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
    }
    return (deliver);
}
 
sub vcl_miss {
    if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
    }
    return (fetch);
}
 
[root@myserver ~]#

Below is reference from my system(See Age:0)

linux@tuxworld:~/Desktop$ curl -I myip.sharadchhetri.com
HTTP/1.1 200 OK
Server: Apache
Content-Type: text/html; charset=UTF-8
Accept-Ranges: bytes
Date: Thu, 03 Oct 2013 16:53:31 GMT
X-Varnish: 2015266685
Age: 0
Via: 1.1 varnish
Connection: keep-alive

linux@tuxworld:~/Desktop$

Leave a Comment

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