[英]Howto control Varnish and a Browser using Cache-Control: max-age Header in a Rails environment?
最近我在Rails應用程序堆棧中添加了一個Varnish實例。 可以通過使用Cache-Control Header緩存某個資源來確定其中的Varnish的默認配置,如下所示:
Cache-Control: max-age=86400, public=true
我使用控制器中的expires_in語句實現了那個:
def index
expires_in 24.hours, public: true
respond_with 'some content'
end
這很好用。 我沒想到的是,Cache-Control標頭也會影響瀏覽器。 這導致了 - Varnish和我的用戶瀏覽器都緩存某個資源的問題。 資源將從varnish中正確清除,但除非達到max-age,否則瀏覽器不會再次嘗試請求它。
所以我想知道我應該將'expires_in'與Varnish結合使用嗎? 我可以在Varnish前面的Nginx或Apache實例中過濾Cache-Control標頭,但這看起來很奇怪。
任何人都可以開導我嗎?
問菲利克斯
這實際上是一個非常好的有效問題,也是一個非常常見的反向代理問題。
問題是只有一個Cache-Control屬性,它適用於客戶端瀏覽器(私有緩存)和/或代理服務器(共享緩存)。 如果您不希望第三方代理完全緩存您的內容,並希望每個請求都由您的Varnish(或您的Rails后端)提供,您必須從Varnish發送適當的Cache-Control標頭。
修改后端發送的Cache-Control標頭將在https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching中詳細討論
您可以從兩個不同的角度處理解決方案。 如果您希望在Rails后端定義max-age,例如為不同的對象指定不同的TTL,則可以使用上面鏈接中描述的方法。
另一個解決方案是不從后端發送Cache-Control頭,而是在varnish vcl_fetch()中為對象定義所需的TTL。 這是我們采取的方法。
我們在Varnish中有一個600秒的默認TTL,並為進行更改時明確清除的頁面定義更長的TTL。 這是我們當前的vcl_fetch()定義:
sub vcl_fetch {
if (req.http.Host ~ "(forum|discus)") {
# Forum pages are purged explicitly, so cache them for 48h
set beresp.ttl = 48h;
}
if (req.url ~ "^/software/") {
# Software pages are purged explicitly, so cache them for 48h
set beresp.ttl = 48h;
}
if (req.url ~ "^/search/forum_search_results" ) {
# We don't want forum search results to be cached for longer than 5 minutes
set beresp.ttl = 300s;
}
if(req.url == "/robots.txt") {
# Robots.txt is updated rarely and should be cached for 4 days
# Purge manually as required
set beresp.ttl = 96h;
}
if(beresp.status == 404) {
# Cache 404 responses for 15 seconds
set beresp.http.Cache-Control = "max-age=15";
set beresp.ttl = 15s;
set beresp.grace = 15s;
}
}
在我們的例子中,我們根本不從Web后端服務器發送Cache-Control頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.