Cache

清漆配置僅記憶體未登錄使用者

  • June 9, 2010

我有一個 varnish+nginx 前面的 Ruby on Rails 應用程序。由於大多數站點內容都是靜態的,除非您是登錄使用者,因此我想在使用者註銷時使用清漆大量記憶體站點,但僅在使用者登錄時記憶體靜態資產。

當使用者登錄時,他們的 Cookie: 標頭中會出現 cookie ‘user_credentials’,此外,我需要跳過 /login 和 /sessions 上的記憶體,以便使用者首先獲得他們的 ‘user_credentials’ cookie .

預設情況下,Rails 不設置記憶體友好的 Cache-control 標頭,但我的應用程序在使用者未登錄時設置了“public,s-max-age=60”標頭。Nginx 設置為返回“遠期”到期標頭對於所有靜態資產。

我目前的配置是在登錄時完全繞過所有內容的記憶體,包括靜態資產——並且在註銷時為所有內容返回記憶體 MISS。我花了幾個小時在圈子裡轉,這是我目前的 default.vcl

   director rails_director round-robin {
 { 
   .backend = { 
     .host = "xxx.xxx.xxx.xxx"; 
     .port = "http";
     .probe = {
       .url = "/lbcheck/lbuptest";
       .timeout = 0.3 s;
       .window = 8;
       .threshold = 3;
     }
   } 
 }
}

sub vcl_recv {

 if (req.url ~ "^/login") {
   pipe;
 }

 if (req.url ~ "^/sessions") {
   pipe;
 }
 # The regex used here matches the standard rails cache buster urls
 # e.g. /images/an-image.png?1234567
 if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") {
   unset req.http.cookie;
   lookup;
 } else {
   if (req.http.cookie ~ "user_credentials") {
     pipe;
   }
 }

 # Only cache GET and HEAD requests
 if (req.request != "GET" && req.request != "HEAD") {
   pipe;
 }

}

sub vcl_fetch {

 if (req.url ~ "^/login") {
   pass;
 }

 if (req.url ~ "^/sessions") {
   pass;
 }

 if (req.http.cookie ~ "user_credentials") {
   pass;
 } else {
   unset req.http.Set-Cookie;
 }

 # cache CSS and JS files
 if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") {
   unset req.http.Set-Cookie;
 } 

 if (obj.status >=400 && obj.status <500) {
   error 404 "File not found";
 }

 if (obj.status >=500 && obj.status <600) {
   error 503 "File is Temporarily Unavailable";
 }

}

sub vcl_deliver {
 if (obj.hits > 0) {
         set resp.http.X-Cache = "HIT";
 } else {
         set resp.http.X-Cache = "MISS";
 }
}

好的,最後我設法使用以下 vcl 文件解決了這個問題。請注意,我添加了一些額外的位以在後端死亡時允許記憶體過期寬限期。

似乎我的主要失敗是unset req.http.Set-Cookie;在我應該unset obj.http.Set-Cookie;在該vcl_fetch部分中使用時使用。(obj在 vcl_fetch 和reqvcl_recv 部分)。

director rails_director round-robin {
 { 
   .backend = { 
     .host = "xxx.xxx.xxx.xxx"; 
     .port = "http";
     .probe = {
       .url = "/lbcheck/lbuptest";
       .timeout = 0.3 s;
       .window = 8;
       .threshold = 3;
     }
   } 
 }
}

sub vcl_recv {

 if (req.backend.healthy) {
   set req.grace = 30s;
 } else {
   set req.grace = 1h;
 }

 if (req.url ~ "^/login") {
   pipe;
 }

 if (req.url ~ "^/sessions") {
   pipe;
 }

 if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") {
   unset req.http.cookie;
   lookup;
 } else {
   if (req.http.cookie ~ "user_credentials") {
     pipe;
   } else {
     unset req.http.cookie;
   }
 }

 # Only cache GET and HEAD requests
 if (req.request != "GET" && req.request != "HEAD") {
   pipe;
 }

}

sub vcl_fetch {

 set obj.grace = 1h;

 if (req.url ~ "^/login") {
   pass;
 }

 if (req.url ~ "^/sessions") {
   pass;
 }

 if (req.http.cookie ~ "user_credentials") {
   pass;
 } else {
   unset obj.http.Set-Cookie;
 }

 # cache CSS and JS files
 if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") {
   unset obj.http.Set-Cookie;
 } 

 if (obj.status >=400 && obj.status <500) {
   error 404 "File not found";
 }

 if (obj.status >=500 && obj.status <600) {
   error 503 "File is Temporarily Unavailable";
 }

}

sub vcl_deliver {
 if (obj.hits > 0) {
         set resp.http.X-Cache = "HIT";
 } else {
         set resp.http.X-Cache = "MISS";
 }
}

引用自:https://serverfault.com/questions/42164