0

How to setup Varnish, so that it does not cache content from a particular folder (and its subfolders), let's say /mnt/var/public_html/useroutput/

I have tried editing /etc/varnish/default.vcl

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 != "GET" &&
  req.request != "HEAD" &&
  req.request != "PUT" &&
  req.request != "POST" &&
  req.request != "TRACE" &&
  req.request != "OPTIONS" &&
  req.request != "DELETE") {
    /* Non-RFC2616 or CONNECT which is weird. */
    return (pipe);
}
    if (req.url ~ "^/useroutput") {
            return (pass);
    }
if (req.request != "GET" && req.request != "HEAD") {
    /* We only deal with GET and HEAD by default */
    return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
    /* Not cacheable by default */
    return (pass);
}
return (lookup);

}

but it's not working.

Please suggest me how to fix it.

Response Header

    Accept-Ranges:bytes
Age:0
Cache-Control:max-age=172800
Connection:keep-alive
Content-Encoding:gzip
Content-Length:10274
Content-Type:text/html; charset=UTF-8
Date:Tue, 13 Sep 2016 11:15:19 GMT
Expires:Thu, 15 Sep 2016 11:15:19 GMT
Server:Apache/2.2.15 (CentOS)
Vary:Accept-Encoding,User-Agent
X-Cache:MISS
X-Powered-By:PHP/5.6.13
X-Varnish:909996209

Request Headers

    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:_csrf=eb7266d5fd58a94f68f73514f2e2940c661222b810bcd214f190efc326cf1d51s%3A32%3A%22kMAh10CYLOhYVlVleXHkRX-aWAqdXytJ%22%3B; PHPSESSID=0tuegrs49ooa0alert6efcu3k7; wp_lead_uid=z3thLcIf6HBdK0GnJcp2c0ZHxfzWLMhUwvh; inbound_referral_site=Direct Traffic; style=null; lead_session=1; _gat=1; _identity=6a6fbb91546910ef2b03dd8c1a5a987a4a988c5915d7ae0c7275cf650ce31bd8s%3A47%3A%22%5B17%2C%22ekSOaz5sJC63xPKhSRTqaENTuc1E_BDh%22%2C1209600%5D%22%3B; _ga=GA1.2.478232635.1473741537
Host:xyz.com
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Vineet kharwar
  • 51
  • 1
  • 1
  • 7

1 Answers1

0

Because you have a "lookup" at the end and it keeps caching, seems that it's not matching your "pass" IF condition. Check if you have to add a * or something to match "/useroutput/something/anotherthing"

In the comment question about know if a page is cached. You can set a cookie to see when an object is HIT/MISS (Im using Varnish 4)

sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT";
  } else {
    set resp.http.X-Cache = "MISS";
  }

  # Remove some headers: Nginx version & OS
  unset resp.http.Via;

  return (deliver);
}
Ayozemr
  • 1
  • 1
  • thanks for the ans. I applied hit miss and it showing miss but still caching .. and used a condition if (req.url ~ "^/useroutput/*") . but no help – Vineet kharwar Sep 13 '16 at 10:57
  • adding response/request header to question – Vineet kharwar Sep 13 '16 at 11:16
  • MISS is an object that can be cached but its not in the varnish store, so your request has to be going through the "lookup". But if you see MISS it should not be cached object. What is your req url? In the other hand, "lookup" means "When you call lookup from vcl_recv you tell Varnish to deliver content from cache even if the request othervise indicates that the request should be passed." Have you tried to change that lookup for a hash? And then do hash_data to req.url, req.http.Content-Type and req.http.Cookie.. Then do lookup. Thats my setup. – Ayozemr Sep 14 '16 at 08:25