varnish缓存三:VCL配置示例

我公司当前线上varnish配置示例:

#backend default {

#.host = "127.0.0.1";

#.port = "8080";

#}


backend s360_01 {

.host = "192.168.0.114";

.port = "18286";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend s360_02 {

.host = "192.168.0.115";

.port = "18286";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend swdj_01 {

.host = "192.168.0.114";

.port = "8092";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend swdj_02 {

.host = "192.168.0.115";

.port = "8092";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend bf_01 {

.host = "192.168.0.111";

.port = "18282";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend bf_02 {

.host = "192.168.0.112";

.port = "18282";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend zsoft_01 {

.host = "192.168.0.111";

.port = "8090";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


backend zsoft_02 {

.host = "192.168.0.112";

.port = "8090";

.probe = {

       .url = "/";

       .interval = 5s;

       .timeout = 1 s;

       .window = 5;

       .threshold = 3;

}

}


director s360 random {

{.backend = s360_01; .weight = 5;}

{.backend = s360_02; .weight = 5;}

}


director wdj random {

{.backend = swdj_01; .weight = 5;}

{.backend = swdj_02; .weight = 5;}

}


director bf random {

{.backend = bf_01; .weight = 5;}

{.backend = bf_02; .weight = 5;}

}


director zsoft random {

{.backend = zsoft_01; .weight = 5;}

{.backend = zsoft_02; .weight = 5;}

}


acl local{

"localhost";

"127.0.0.1";

"192.168.0.0"/24;

}


sub vcl_recv {


if(req.url ~ "^/s360/") {

set req.backend = s360;

}else if(req.url ~ "^/swdj/"){

set req.backend = wdj;

}else if(req.url ~ "^/bf/"){

set req.backend = bf;

}else if(req.url ~ "^/zsoft/"){

set req.backend = zsoft;

#}else{

#set req.backend = default.

}


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.http.Accept-Encoding) {

if (req.url ~ "\.(jpg|png|gif|jpeg|flv)" ) {

remove req.http.Accept-Encoding;

remove req.http.Cookie;

} else if (req.http.Accept-Encoding ~ "gzip") {

set req.http.Accept-Encoding = "gzip";

} else if (req.http.Accept-Encoding ~ "deflate") {

set req.http.Accept-Encoding = "deflate";

} else {

remove req.http.Accept-Encoding;

}

}


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.http.Authenticate || req.http.Authorization) {

return (pass);

}


if(req.request == "PURGE"){

if(!client.ip ~ local){

error 405 "Not Allowed.";

}

return (lookup);

}


if(req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico|js|mp4|css)$"){

unset req.http.cookie;

set req.url = regsub(req.url, "\?.*$", "");

}


set req.grace = 15s;

return (lookup);

}


sub vcl_pipe {

return (pipe);

}


sub vcl_pass {

return (pass);

}


sub vcl_hash {

hash_data(req.url);

if (req.http.host) {

hash_data(req.http.host);

} else {

hash_data(server.ip);

      }

return (hash);

}


sub vcl_miss {

if(req.request == "PURGE") {

error 404 "not in cache.";

}

return (fetch);

}


sub vcl_hit {

if(req.request == "PURGE"){

purge;

error 200 "purged";

}

return (deliver);

}


sub vcl_fetch {


if (beresp.http.Pragma ~"no-cache" ||

beresp.http.Cache-Control ~"no-cache" ||

beresp.http.Cache-Control ~"private") {

return (deliver);

}


if (beresp.status == 404 || beresp.status == 300){

error 404;

}


if (req.request == "GET" && req.url ~ "^/s360(.*)$") {  

   set beresp.ttl = 3600s;  

}


if (req.request == "GET" && req.url ~ "^/swdj(.*)$") {

           set beresp.ttl = 43200s;

       }


if (req.request == "GET" && req.url ~ "^/bf(.*)$") {

           set beresp.ttl = 43200s;

       }


if (req.request == "GET" && req.url ~ "^/zsoft(.*)$") {

           set beresp.ttl = 43200s;

       }


if (req.request == "GET" && req.url ~ ".(html|htm)$"){

set beresp.ttl = 1d;

}


if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)$") {  

set beresp.ttl = 7d;  

}


set beresp.grace = 30m;

return(deliver);

}


sub vcl_deliver {

set resp.http.x-hits = obj.hits;

if(obj.hits > 0){

set resp.http.X-Cache = " HIT 360";

}else{

set resp.http.X-Cache = "MISS 360";

}

unset resp.http.X-Powered-By;

unset resp.http.Server;

return (deliver);

}


sub vcl_error {

    set obj.http.Content-Type = "text/html; charset=utf-8";

    set obj.http.Retry-After = "5";

    synthetic {"

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

  <head>

    <title>"} + obj.status + " " + obj.response + {"</title>

  </head>

  <body>

    <h1>Error "} + obj.status + " " + obj.response + {"</h1>

    <p>"} + obj.response + {"</p>

    <h3>Guru Meditation:</h3>

    <p>XID: "} + req.xid + {"</p>

    <hr>

    <p>Varnish cache server</p>

  </body>

</html>

"};

    return (deliver);

}


sub vcl_init {

  return (ok);

}


sub vcl_fini {

  return (ok);

}


你可能感兴趣的:(缓存,varnish,VCL,配置示例)