How to specify varnish for caching certain file types

I have a dedicated server that hosts my own websites. I installed varnish with default VCL file. Now I want to tell varnish to do the following:

  • Load only the following static file types (.js, .css, .jpg, .png, .gif, .jpg). These are server-side file types that are served, not URLs ending with these extensions.
  • Don't cache files larger than 1 MB
  • Any file must expire in 1 day (or any other period).
  • Caching can only happen when Apache sends 200 HTTP code.

Otherwise, leave the request intact so that it can be served by Apache or any other backend.

What should I write in the VCL file to achieve these requirements? Or what should I do?

+3


source to share


2 answers


What I've done:

1- Isolate all static content in a different domain (that is, the domain for serving dynamic pages is different from the domain for static content).

2- Assign another dedicated IP address to the domain that serves static content

3. Tell varnish to only listen to this IP (i.e. static IP content) on port 80

4- Using Apache to manage the cache period for each static content type (varnish will just obey these headers)

minuses:



1 Varnish will not even listen to or process requests that he should leave unchanged. These requests (for dynamic pages) go directly to Apache since Apache is listening on the original IP address (performance).

2 There is no need to modify the default VCL standard file (only if you want to debug) and this is useful for those who do not know the principles of the VCL language.

3- You control everything from Apache conf.

Pros:

1- You should buy a new dedicated IP if you don't have a spare one.

thank

+2


source


You can do all this in the vcl_fetch subroutine. This should be considered pseudocode.



if (beresp.http.content-type ~ "text/javascript|text/css|image/.*") {
    if (std.integer(beresp.http.Content-Length,0) < /* max size in bytes here */ ) {
        if (beresp.status == 200) { /*  backend returned 200 */
            set obj.ttl = 86400; /* cache for one day */
            return (deliver);
        }
    }
} 
set obj.ttl = 120;
return (hit_for_pass); /* won't be cached */

      

+2


source







All Articles