2015-11-21 141 views
1

嗨,我嘗試在Ubuntu配置清漆4 12.04清漆4×緩存未命中

這裏是我的清漆配置

須藤更多的/ etc /默認/清漆

# Configuration file for Varnish Cache. 
# 
# /etc/init.d/varnish expects the variables $DAEMON_OPTS, $NFILES and $MEMLOCK 
# to be set from this shell script fragment. 
# 
# Note: If systemd is installed, this file is obsolete and ignored. You will 
# need to copy /lib/systemd/system/varnish.service to /etc/systemd/system/ and 
# edit that file. 

# Should we start varnishd at boot? Set to "no" to disable. 
START=yes 

# Maximum number of open files (for ulimit -n) 
NFILES=131072 

# Maximum locked memory size (for ulimit -l) 
# Used for locking the shared memory log in memory. If you increase log size, 
# you need to increase this number as well 
MEMLOCK=82000 

DAEMON_OPTS="-a :80 \ 
      -T localhost:6082 \ 
      -f /etc/varnish/default.vcl \ 
      -S /etc/varnish/secret \ 
      -s malloc,5G" 

更多的/ etc /清漆/ default.vcl

# 
# This is an example VCL file for Varnish. 
# 
# It does not do anything by default, delegating control to the 
# builtin VCL. The builtin VCL is called when there is no explicit 
# return statement. 
# 
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ 
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples. 

# Marker to tell the VCL compiler that this VCL has been adapted to the 
# new 4.0 format. 
vcl 4.0; 

# Default backend definition. Set this to point to your content server. 
backend default { 
    .host = "127.0.0.1"; 
    .port = "8080"; 
} 

sub vcl_recv { 

# Only cache GET or HEAD requests. This makes sure the POST requests are always passed. 
# if (req.method != "GET" && req.method != "HEAD") { 
# return (pass); 
# } 

# Strip hash, server doesn't need it. 
    if (req.url ~ "\#") { 
    set req.url = regsub(req.url, "\#.*$", ""); 
    } 


if (req.http.Cache-Control ~ "(?i)no-cache") { 
if (! (req.http.Via || req.http.User-Agent ~ "(?i)bot" || req.http.X-Purge)) { 
# return(purge); # Couple this with restart in vcl_purge and X-Purge header to avoid loops 
unset req.http.Cache-Control; 
} 
} 





# removes all cookies named _ and google analistics, tracking, utma, utmb...) 
set req.http.Cookie = regsuball(req.http.Cookie, "(^|(?<=;)) *_.=[^;]+;? *", "\1"); 
set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(;)?", ""); 
    set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(;)?", ""); 
    set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(;)?", ""); 

    # Are there cookies left with only spaces or that are empty? 
    if (req.http.cookie ~ "^\s*$") { 
    unset req.http.cookie; 
    } 

# Large static files are delivered directly to the end-user without 
    # waiting for Varnish to fully read the file first. Varnish 4 fully supports Streaming, so set do_stream in vcl_backend_response() 
    if (req.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip|bz2|xz|7z|avi|mov|ogm|mpe?g|mk[av]|webm)(\?.*)?$") { 
    unset req.http.Cookie; 
    return (hash); 
    } 

# Remove all cookies for static files 

if (req.url ~ "^[^?]*\.(bmp|mov|mp4|webm|avi|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|pdf|png|rtf|swf|txt|woff|xml)(\?.*)?$") { 
    unset req.http.Cookie; 
    return (hash); 
    } 


    # Happens before we check if we have this in cache already. 
    # 
    # Typically you clean up the request here, removing cookies you don't need, 
    # rewriting the request, etc. 

set req.http.Surrogate-Capability = "abc=ESI/1.0"; 

if (req.http.X-Forwarded-Proto == "https") { 
     set req.http.X-Forwarded-Port = "443"; 
    } else { 
     set req.http.X-Forwarded-Port = "80"; 
    } 

} 

sub vcl_backend_response { 
    # Happens after we have read the response headers from the backend. 
    # 
    # Here you clean the response headers, removing silly Set-Cookie headers 
    # and other mistakes your backend does. 

if (beresp.http.Surrogate-Control ~ "ESI/1.0") { 
     unset beresp.http.Surrogate-Control; 
     set beresp.do_esi = true; 
    } 

# Enable cache for all static files 
if (bereq.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|mp[34]|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|woff|xml|zip|webm)(\?.*)?$") { 
    unset beresp.http.set-cookie; 
    } 

# Large static files are delivered directly to the end-user without 
if (bereq.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip|bz2|xz|7z|avi|mov|ogm|mpe?g|mk[av]|webm)(\?.*)?$") { 
    unset beresp.http.set-cookie; 
    set beresp.do_stream = true; # Check memory usage it'll grow in fetch_chunksize blocks (128k by default) if the backend doesn't send a Content-Length header, s 
o only enable it for big objects 
    set beresp.do_gzip = false; # Don't try to compress it for storage 
    } 

if (bereq.http.Cookie ~ "(UserID|_session)") { 
     set beresp.http.X-Cacheable = "NO:Got Session"; 
     set beresp.uncacheable = true; 
     return (deliver); 

} elsif (beresp.ttl <= 0s) { 
     # Varnish determined the object was not cacheable 
     set beresp.http.X-Cacheable = "NO:Not Cacheable"; 

} elsif (beresp.http.set-cookie) { 
     # You don't wish to cache content for logged in users 
     set beresp.http.X-Cacheable = "NO:Set-Cookie"; 
     set beresp.uncacheable = true; return (deliver); 
     return (deliver); 

} elsif (beresp.http.Cache-Control ~ "private") { 
     # You are respecting the Cache-Control=private header from the backend 
     set beresp.http.X-Cacheable = "NO:Cache-Control=private"; 
     set beresp.uncacheable = true; 

} else { 
     # Varnish determined the object was cacheable 
     set beresp.http.X-Cacheable = "YES"; 
    } 
     return (deliver); 
} 

sub vcl_deliver { 
    # Happens when we have all the pieces we need, and are about to send the 
    # response to the client. 
    # 
    # You can do accounting or modifying the final object here. 

if (obj.hits > 0) { # Add debug header to see if it's a HIT/MISS and the number of hits, disable when not needed 
    set resp.http.X-Cache = "HIT"; 
    } else { 
    set resp.http.X-Cache = "MISS"; 
    } 

set resp.http.XH = obj.hits; 

unset resp.http.X-Varnish; 
unset resp.http.Via; 

} 

我看圖像緩存

響應頭

Accept-Ranges:bytes 
Age:115 
Connection:keep-alive 
Content-Length:2703 
Content-Type:image/jpeg 
Date:Sat, 21 Nov 2015 09:59:42 GMT 
ETag:"a8f-523e3697bdc69" 
Last-Modified:Fri, 06 Nov 2015 18:28:37 GMT 
Server:Apache/2.4.7 (Ubuntu) 
X-Cache:HIT 
X-Cacheable:YES 
XH:7 

請求頭 (我禁用瀏覽器緩存,清漆應該忽略它)

GET /media/cache/mini_thumb/images/nofoto.jpg HTTP/1.1 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 
Accept: image/webp,image/*,*/*;q=0.8 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 
    Accept-Encoding: gzip, deflate, sdch 
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4,pt;q=0.2,de;q=0.2,sk;q=0.2 

當我刷新在其他瀏覽器頁面,我得到響應頭:

HTTP/1.1 200 OK 
Date: Sat, 21 Nov 2015 10:11:36 GMT 
Server: Apache/2.4.7 (Ubuntu) 
Last-Modified: Fri, 06 Nov 2015 18:28:37 GMT 
ETag: "a8f-523e3697bdc69" 
Content-Length: 2703 
Content-Type: image/jpeg 
X-Cacheable: YES 
Age: 0 
X-Cache: MISS 
XH: 0 
Accept-Ranges: bytes 
Connection: keep-alive 

可能是什麼原因

X-緩存:MISS XH:0

+0

這裏有什麼進展?正如我在下面寫的那樣,檢查響應的「Vary」標題是絕對值得的。 – Laizer

回答

0

標題中缺少Expires標記 - 添加時,清漆開始緩存。

+0

添加了[添加過期標頭](https://stackoverflow.com/a/13972671/3898339)仍然沒有運氣。這篇文章是否包含使用.htaccess添加標題的正確方法 –

0

是值得的檢查,如果您的後臺返回一個Vary頭。例如,如果它返回Vary: Accept-Language,則請求的Accept-Language字符串中的細微差異會導致緩存未命中。有關該主題,請參閱Varnish docs