嗨,我尝试在 ubuntu 12.04 中配置清漆 4
这是我的清漆配置
sudo 更多 /etc/default/varnish
# 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/varnish/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-缓存:小姐 XH:0