Jan 232013
 

Une configuration basique de varnish :

# Backend par defaut
backend default {
   .host = "127.0.0.1";
   .port = "81";
   .connect_timeout = 1s;
   .first_byte_timeout = 30s;
     .probe = {
        .url = "/";  # ou .request = "GET / HTTP/1.1" "Host: blog.jeremm.fr" "Connection: close";
        .timeout  = 15s;
        .interval = 15s;
        .window    = 5;
        .threshold = 2;
    }
}
 
sub vcl_recv {
   set req.grace = 300s;
   set req.backend = default ;
   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") {
        return (pipe);
   }
   if (req.request != "GET" && req.request != "HEAD") {
       return (pass);
   }
   if (req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|gz|rar|txt|bzip|pdf)(\?.*|)$" && req.url !~ "^/index.php?") {
       unset req.http.Cookie;
       return (lookup);
   }
   if (req.http.Authorization) {
       return (pass);
   }
   return (lookup);
}
 
sub vcl_hash {
        hash_data(req.url);
        if (req.http.host) {
                hash_data(req.http.host);
        } else {
                hash_data(server.ip);
        }
        if (req.http.Cookie) {
                hash_data(req.http.Cookie);
        }
        return(hash);
}
sub vcl_pass {
        set req.http.X-marker = "pass" ;
}
sub vcl_fetch {
        if ( ! beresp.http.Content-Encoding ~ "gzip" ) {
                set beresp.do_gzip = true;
        }
        if (req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|gz|rar|txt|bzip|pdf)$") {
                unset beresp.http.Set-Cookie;
        }
        if (beresp.ttl > 0s ) {
                  if (beresp.status >= 300 && beresp.status <= 399) {
                        set beresp.ttl = 10m;
                  }
                  if (beresp.status >= 399) {
                        set beresp.ttl = 0s;
                  }
        }
        if (beresp.status >= 399) {
                unset beresp.http.Set-Cookie;
        }
        # Maximum 24h de cache
        if (beresp.ttl > 86400s) {
                set beresp.ttl = 86400s;
        }
        if (req.http.X-marker == "pass" ) {
                unset req.http.X-marker;
                set beresp.http.X-marker = "pass";
                set beresp.ttl = 0s ;
        }
        if (beresp.ttl > 0s && beresp.http.Set-Cookie) {
                set beresp.ttl = 0s ;
        }
}
 
sub vcl_deliver {
        if (obj.hits > 0){
                set resp.http.X-Varnish-Cache = "HIT";
        }else{
                set resp.http.X-Varnish-Cache = "MISS";
        }
        if (resp.http.X-marker == "pass" ) {
                remove resp.http.X-marker;
                set resp.http.X-Varnish-Cache = "PASS";
        }
        remove resp.http.Via;
        remove resp.http.X-Varnish;
        remove resp.http.Server;
        remove resp.http.X-Powered-By;
 }
 
sub vcl_error {
        if (obj.status >= 500 && req.restarts < 4) {
                return (restart);
        }
}

Pour télécharger directement le fichier default.vcl : default.vcl

Pour les options de démarrage : Configuration et optimisation du daemon Varnish

Maj 06/03/12 : ajout du X-marker pass
Maj 16/04/12 : ajout req.grace
Maj 19/08/12 : téléchargement du fichier default.vcl / don’t hash Accept-Encoding
Maj 03/09/12 : unset set-cookie sur media, pas de unset cookie sur media commençant par /index.php?, unset Set-cookie sur 404
Maj 23/01/13 : add set beresp.do_gzip = true ;

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

(required)

(required)

Spam protection by WP Captcha-Free