Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. backend web01 {
  2.   .host = "192.168.30.101";
  3.   .port = "80";
  4.   .connect_timeout = 1s;
  5.   .first_byte_timeout = 5s;
  6.   .probe = {
  7.     .url = "/";
  8.     .timeout  = 10s;
  9.     .interval = 30s;
  10.     .window = 2;
  11.     .threshold = 2;
  12.   }
  13. }
  14.  
  15. backend web02 {
  16.   .host = "192.168.30.102";
  17.   .port = "80";
  18.   .connect_timeout = 1s;
  19.   .first_byte_timeout = 5s;
  20.   .probe = {
  21.     .url = "/";
  22.     .timeout  = 10s;
  23.     .interval = 30s;
  24.     .window = 2;
  25.     .threshold = 2;
  26.   }
  27. }
  28.  
  29. backend web03 {
  30.   .host = "192.168.30.103";
  31.   .port = "80";
  32.   .connect_timeout = 1s;
  33.   .first_byte_timeout = 5s;
  34.   .probe = {
  35.     .url = "/";
  36.     .timeout  = 10s;
  37.     .interval = 30s;
  38.     .window = 2;
  39.     .threshold = 2;
  40.   }
  41. }
  42.  
  43.  
  44. director web_cluster round-robin {
  45.  { .backend = web01; }
  46.  { .backend = web02; }
  47.  { .backend = web03; }
  48. }
  49.  
  50. #deflate logic ~ call normalize_deflate;
  51. sub normalize_deflate {
  52.   if (req.http.Accept-Encoding) {
  53.     if (req.http.Accept-Encoding ~ "gzip") {
  54.       set req.http.Accept-Encoding = "gzip";
  55.     }
  56.     elsif (req.http.Accept-Encoding ~ "deflate") {
  57.       set req.http.Accept-Encoding = "deflate";
  58.     }
  59.     else {
  60.       remove req.http.Accept-Encoding;
  61.     }
  62.   }
  63. }
  64.  
  65. #normalize request to be cached ~ call recv_cache_prep;
  66. sub recv_cache_prep {
  67.   unset req.http.cookie;
  68.   unset req.http.user-agent;
  69.   return(lookup);
  70. }
  71.  
  72. #normalize response to be cached ~ call fetch_cache_prep;
  73. sub fetch_cache_prep {
  74.   unset beresp.http.last-modified;
  75.   unset beresp.http.set-cookie;
  76.   unset beresp.http.expires;
  77.   unset beresp.http.etag;
  78.   set beresp.http.cache-control = "max-age = 3600";
  79. }
  80.  
  81. #set our objects to brand new and add our X-cache header
  82. sub vcl_deliver {
  83.   set resp.http.age = "0";
  84.   if (obj.hits > 0) {
  85.     set resp.http.X-Cache = "m: HIT " obj.hits;
  86.   }
  87.   else {
  88.     set resp.http.X-Cache = "m: MISS";
  89.   }
  90. }
  91.  
  92. #restart if we can't get a valid response
  93. sub vcl_error {
  94.   if (obj.status == 503 && req.restarts >= 5) {
  95.     restart;
  96.   }
  97. }
  98.  
  99. sub vcl_recv {
  100.  
  101.   set req.http.X-Forwarded-For = client.ip;
  102.  
  103.   #misc settings
  104.   set req.grace = 1h;
  105.  
  106.   #deflate logic
  107.   call normalize_deflate;
  108.  
  109.   #set our backend
  110.   set req.backend = web_cluster;
  111.  
  112.   #static files
  113.   if (req.url ~ "^/[^?]+\.((?i)bmp|jpeg|jpg|png|gif|ico|js|css|txt|zip|swf|pdf)(\?.*|)$") {
  114.     set req.url = regsub(req.url, "\?.*$", "");
  115.     set req.http.X-v_cache = "file";
  116.     call recv_cache_prep;
  117.   }
  118.   #no caching
  119.   else {
  120.     unset req.http.if-modified-since;
  121.     return(pass);
  122.   }
  123.  
  124. }
  125.  
  126. sub vcl_fetch {
  127.  
  128.   #misc settings
  129.   set beresp.grace = 1h;
  130.  
  131.   #static file  
  132.   if (req.http.X-v_cache == "file") {
  133.     call fetch_cache_prep;
  134.     set beresp.ttl = 12h;
  135.   }
  136.   #don't cache 404's or redirects
  137.   elsif (beresp.status == 404 || beresp.status == 302 || beresp.status == 301) {
  138.     set beresp.cacheable = false;
  139.     return(pass);
  140.   }
  141.   #no caching
  142.   else {
  143.     set beresp.cacheable = false;
  144.     return(pass);
  145.   }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement