4

How to configure varnish(or if someone can hint me to squid im fine) to cache requests from backend, but connect to backend through http_proxy

So I try to:

backend default {
    .host = "10.1.1.1";
    .port = "8080";
}

backend corp_proxy {
  .host = "proxy";
  .port = "8080";
}
sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    set req.backend_hint = corp_proxy;
    set req.url ="http://" + req.http.host + req.url;
}
mAm
  • 177
  • 1
  • 13

1 Answers1

2

Varnish (or other web caching proxies) caches a request, based on its cache-related headers (like Cache-Control).
Unfortunately, many web applications don't set these headers correctly. So we should use a more aggressive approach to cache some well-known items, e.g., pictures, .js or .css files.

Moreover, this line set req.url ="http://" + req.http.host + req.url; is not required, because Varnish sends the request, as is, to your designated backend.

Here is my recommended config:

backend corp_proxy {
  .host = "proxy";
  .port = "8080";
}

sub vcl_recv {
  
  // Determine backend
  if ( req.http.host ~ ".example.com" ) {
    set req.backend_hint = corp_proxy;
    
    // Determine cacheable items
    if( req.url ~ "\.(css|js|jpg|jpeg|png|gif|ico) {
      unset req.http.Cookie;
      unset req.http.Cache-Control;
    }
  }
  
}

sub vcl_backend_response {
  
  if( bereq.http.host ~ ".example.com" ) {
    if (bereq.url ~ "\.(css|jsjpg|jpeg|png|gif|ico)") {
      set beresp.ttl = 20m; // I opt for 20 minutes of caching by your mileage may vary
  }

}

hayoola
  • 121
  • 1
  • 6