2012-10-12 77 views
3

我使用光油3.0.3並通過在靜態資源的HTTP標頭中設置最大年齡來利用瀏覽器緩存。我嘗試添加下列配置。default.vcl:光油3 - 如何在http標題中設置最大年齡

sub vcl_fetch { 
    if (beresp.cacheable) { 
    /* Remove Expires from backend, it's not long enough */ 
    unset beresp.http.expires; 

    /* Set the clients TTL on this object */ 
    set beresp.http.cache-control = "max-age=900"; 

    /* Set how long Varnish will keep it */ 
    set beresp.ttl = 1w; 

    /* marker for vcl_deliver to reset Age: */ 
    set beresp.http.magicmarker = "1"; 
    } 
} 

sub vcl_deliver { 
    if (resp.http.magicmarker) { 
    /* Remove the magic marker */ 
    unset resp.http.magicmarker; 

    /* By definition we have a fresh object */ 
    set resp.http.age = "0"; 
    } 
} 

這是從https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching複製。也許我只是犯了一個錯字。重新啓動Varnish後,它不再起作用。

我有兩個問題。這是用於清漆3的正確方法嗎?如果是這樣,我做錯了什麼?其次,在重啓之前有沒有辦法測試Varnish配置文件? 「Apache/sbin/service httpd configtest」的一些方法。在上線之前就會發現錯誤。謝謝。

回答

2

是的,通常這是覆蓋後端的TTL的方式。 刪除beresp.http.expires,設置beresp.http.cache-control,設置beresp.ttl。 beresp.cacheable是一個2. [01]主義。 3.0中的相同測試是檢查beresp.ttl> 0.

一個小技巧是將你的魔法標記存儲在req.http上,然後在將對象交給該對象之前不必清理它客戶。

關於測試配置文件,您可以直接使用「varnishd -C -f /etc/varnish/default.vcl」來調用VCL編譯器。如果你的VCL有問題,你會得到錯誤信息,如果VCL有效,你會得到幾頁生成的C代碼。

相關問題