2012-04-17 88 views
5

我有一點麻煩讓清漆緩存正確。光油不斷丟失緩存,cookies?

當我進入頁面並點擊刷新時,清漆將返回緩存頁面。 但是,如果我使用另一臺電腦並再次點擊同一頁面(在緩存時間範圍內),它會報告一個MISS。

我用的cPanel清漆插件從http://www.unixy.net/varnish
我刪除的session_start()從我的(PHP)網頁,我並不需要基於用戶的cookie。
我也在使用谷歌分析。

這是我default.vcl

################################################### 
# Copyright (c) UNIXY - http://www.unixy.net # 
# The leading truly fully managed server provider # 
################################################### 

include "/etc/varnish/cpanel.backend.vcl"; 

include "/etc/varnish/backends.vcl"; 

sub vcl_recv { 

# Use the default backend for all other requests 
set req.backend = default; 

# Setup the different backends logic 
include "/etc/varnish/acllogic.vcl"; 

# Allow a grace period for offering "stale" data in case backend lags 
set req.grace = 5m; 

remove req.http.X-Forwarded-For; 
set req.http.X-Forwarded-For = client.ip; 

# cPanel URLs 
include "/etc/varnish/cpanel.url.vcl"; 

# Properly handle different encoding types 
if (req.http.Accept-Encoding) { 
    if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|ico)$") { 
     # No point in compressing these 
     remove req.http.Accept-Encoding; 
    } elsif (req.http.Accept-Encoding ~ "gzip") { 
     set req.http.Accept-Encoding = "gzip"; 
    } elsif (req.http.Accept-Encoding ~ "deflate") { 
     set req.http.Accept-Encoding = "deflate"; 
    } else { 
     # unkown algorithm 
     remove req.http.Accept-Encoding; 
    } 
} 

# Set up disabled 
include "/etc/varnish/disabled.vcl"; 

# Exclude upgrade, install, server-status, etc 
include "/etc/varnish/known.exclude.vcl"; 

# Set up exceptions 
include "/etc/varnish/url.exclude.vcl"; 

# Set up exceptions 
include "/etc/varnish/vhost.exclude.vcl"; 

# Set up vhost+url exceptions 
include "/etc/varnish/vhosturl.exclude.vcl"; 

# Set up cPanel reseller exceptions 
include "/etc/varnish/reseller.exclude.vcl"; 

# Restart rule for bfile recv 
include "/etc/varnish/bigfile.recv.vcl"; 


if (req.request == "PURGE") { 
     if (!client.ip ~ acl127_0_0_1) {error 405 "Not permitted";} 
     return (lookup); 
} 

## Default request checks 
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); 
} 

## Modified from default to allow caching if cookies are set, but not http auth 
if (req.http.Authorization) { 
    return (pass); 
} 

include "/etc/varnish/versioning.static.vcl"; 

## Remove has_js and Google Analytics cookies. 
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", ""); 

set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", ""); 



if (req.http.Cookie ~ "^\s*$") { 
    unset req.http.Cookie; 
} 

include "/etc/varnish/slashdot.recv.vcl"; 

# Cache things with these extensions 
if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|pdf)$" && ! (req.url ~ "\.(php)")) { 
    unset req.http.Cookie; 
    return (lookup); 
} 

return (lookup); 
} 

sub vcl_fetch { 

set beresp.ttl = 40s; 
set beresp.http.Server = " - Web acceleration by http://www.unixy.net/varnish "; 

# Turn off Varnish gzip processing 
include "/etc/varnish/gzip.off.vcl"; 

# Grace to allow varnish to serve content if backend is lagged 
set beresp.grace = 5m; 

# Restart rule bfile for fetch 
include "/etc/varnish/bigfile.fetch.vcl"; 

# These status codes should always pass through and never cache. 
if (beresp.status == 503 || beresp.status == 500) { 
    set beresp.http.X-Cacheable = "NO: beresp.status"; 
    set beresp.http.X-Cacheable-status = beresp.status; 
    return (hit_for_pass); 
} 

if (beresp.status == 404) { 
    set beresp.http.magicmarker = "1"; 
    set beresp.http.X-Cacheable = "YES"; 
    set beresp.ttl = 20s; 
    return (deliver); 
} 

/* Remove Expires from backend, it's not long enough */  
unset beresp.http.expires; 

if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|pdf|ico)$" && ! (req.url ~ "\.(php)")) { 
    unset beresp.http.set-cookie; 
    include "/etc/varnish/static.ttl.vcl"; 
} 
include "/etc/varnish/slashdot.fetch.vcl"; 
else { 
    include "/etc/varnish/dynamic.ttl.vcl"; 
} 

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

# All tests passed, therefore item is cacheable 
set beresp.http.X-Cacheable = "YES"; 

return (deliver); 
} 

sub vcl_deliver { 

    # From http://varnish-cache.org/wiki/VCLExampleLongerCaching 
    if (resp.http.magicmarker) { 
    /* Remove the magic marker */ 
    unset resp.http.magicmarker; 

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

    #add cache hit data 
    if (obj.hits > 0) { 
    #if hit add hit count 
    set resp.http.X-Cache = "HIT"; 
    set resp.http.X-Cache-Hits = obj.hits; 
    } 
else { 
    set resp.http.X-Cache = "MISS"; 
    } 

} 

sub vcl_error { 

if (obj.status == 503 && req.restarts < 5) { 
set obj.http.X-Restarts = req.restarts; 
return (restart); 
} 

} 

# Added to let users force refresh 
sub vcl_hit { 

if (obj.ttl < 1s) { 
    return (pass); 
} 

if (req.http.Cache-Control ~ "no-cache") { 
# Ignore requests via proxy caches, IE users and badly behaved crawlers 
# like msnbot that send no-cache with every request. 
if (! (req.http.Via || req.http.User-Agent ~ "bot|MSIE|HostTracker")) { 
    set obj.ttl = 0s; 
    return (restart); 
} 
} 

return (deliver); 

} 

sub vcl_hash { 

    hash_data(req.http.cookie); 
} 

我的頭文件(第二臺計算機上):

http://www.subtitleseeker.com/ 

GET/HTTP/1.1 
Host: www.subtitleseeker.com 
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive 
Referer: http://www.subtitleseeker.com/search/ 
Cookie: __utma=107027200.157820436.1334670333.1334670333.1334670333.1; __utmb=107027200.51.10.1334670333; __utmz=107027200.1334670333.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=107027200; __unam=4ca9c7b-136c095f22c-1396bc94-1 

HTTP/1.1 200 OK 
X-Powered-By: PHP/5.3.8 
Vary: User-Agent,Accept-Encoding 
Content-Encoding: gzip 
Content-Type: text/html 
Server: - Web acceleration by http://www.unixy.net/varnish 
X-Cacheable: YES 
Content-Length: 8169 
Accept-Ranges: bytes 
Date: Tue, 17 Apr 2012 14:10:12 GMT 
X-Varnish: 916645159 
Via: 1.1 varnish 
Connection: keep-alive 
Age: 0 
X-Cache: MISS 

有人能向我解釋爲什麼這是發生什麼,我應該改變?

謝謝!

回答

2

這是因爲Google Analytics(分析)。即使您的Web應用程序不使用JavaScript,您的分析JavaScript也會使用它。結果是一樣的,Varnish會將請求傳遞給後端,並避免使用緩存。

要解決這個問題,需要在您的Web應用程序中定義您需要使用Cookie的URL(例如管理面板)以及您不需要的地方(在這裏,您可以忽略Google Analytics的要求,大多數Web分析工具只需要cookie瀏覽器和JavaScript)。

下面你可以看到一個Varnish配置文件的例子。有趣的部分是,對於您的網站的非管理部分:發送cookie將被刪除,以及一些瀏覽器請求標頭的新鮮內容。

sub vcl_recv { 
    # regex to find all URLs where cookies are required 
    if (req.url ~ "^/admin/") { 
    # administration panel 
    set req.http.admin = 1; 
    } else { 
    # public web site, ignore client request for fresh content, remove cookies 
    unset req.http.Cache-Control; 
    unset req.http.Max-Age; 
    unset req.http.Pragma; 
    unset req.http.Cookie; 
    } 
    ... 
} 

sub vcl_fetch { 
    if (req.http.admin == 1) { 
    # administration panel 
    return (hit_for_pass); 
    } else { 
    # public web site, not allowed to set cookies 
    unset beresp.http.Set-Cookie; 
    ... 
    } 
    ... 
} 
+0

以下代碼在默認情況下不處理google cookie嗎? ##刪除has_js和Google Analytics Cookie。 set req.http.Cookie = regsuball(req.http.Cookie,「(^ |; \ s *)(__ [a-z] + | has_js)= [^;] *」,「」); – 2012-04-17 15:07:32

+0

通過它的外觀,這確實是一個有效的正則表達式去除這些cookie。但是從varnishlog的輸出中,這並沒有發生。在其中一個包括返回(通過)或返回(管道)被觸發的任何機會? – Mojah 2012-04-17 15:53:52

+0

你說得對,我怎麼會錯過這個。 – 2012-04-17 15:59:48

1

可以使用varnishlog命令行工具來觀看什麼光油做,因爲它處理每個請求。例如,您可以查看是否有任何Cookie正在分裂。

我有一種感覺,它可能是用戶代理在另一臺計算機上略有不同。如果知道的話,清漆將這個值包含在關鍵字中。我們已經在我們vcl_recv的規則是:

set req.http.User-Agent = ""; 

其中固定了類似的問題我們。

+0

(在這個解決方案中IE瀏覽器可能會遇到麻煩..只是規範化它以避免問題,_if(req.http.User-Agent〜「MSIE」){set req.http.User-Agent =「MSIE」;} else {set req.http.User-Agent =「Mozilla compatible」;} _對不起英文 – BrenoZan 2014-02-17 14:14:36