Problem with Varnish X-Cache: MISS all the time
jack Linux
ilinuxer.85 at gmail.com
Mon May 14 07:40:41 UTC 2018
I Have website running on cloudflare + nginx + varnish on centos 6.9
arch 64bit Distribution
============
Header status =
============
HTTP/1.1 200 OK
Date: Mon, 14 May 2018 07:25:34 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=d0d5a5c3e3423f286ca476d1fecbeeb7a1526282734;
expires=Tue, 14-May-19 07:25:34 GMT; path=/; domain=.site.com;
HttpOnly
X-Powered-By: PHP/5.4.45
Set-Cookie: PHPSESSID=4a3847f117e1a757e8b3f92784987bfd; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, must-revalidate
Pragma: no-cache
Set-Cookie: detect_mobiles=20; expires=Tue, 15-May-2018 00:08:54 GMT; path=/
Vary: Accept-Encoding
X-Varnish: 1615241683
Age: 0
Via: 1.1 varnish
X-Cache: MISS
Set-Cookie: site.com=web01; path=/
Server: cloudflare
CF-RAY: 41aba1b134769c35-AMS
=============
= varnish config =
=============
/* SET THE HOST AND PORT OF SITE
* *********************************************************/
backend default {
.host = "10.0.0.1";
.port = "8080";
}
# SET THE ALLOWED IP OF PURGE REQUESTS
# ##########################################################
acl purge {
"localhost";
"10.0.0.1";
}
#THE RECV FUNCTION
# ##########################################################
sub vcl_recv {
#remove HTTPOXY CGI vulnerability
unset req.http.proxy;
#remove extraneous host ports
set req.http.host = regsub(req.http.Host, ":[0-9]+", "");
# set realIP by trimming CloudFlare IP which will be used for
various checks
set req.http.X-Actual-IP = regsub(req.http.X-Forwarded-For,
"[, ].*$", "");
# Enable smart refreshing
if (req.http.Cache-Control ~ "no-cache" && client.ip ~ purge) {
set req.hash_always_miss = true;
}
# Unset cloudflare cookies
# Remove has_js and CloudFlare/Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie,
"(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
# For Testing: If you want to test with Varnish passing (not
caching) uncomment
# return( pass );
# FORWARD THE IP OF THE REQUEST
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
# DO NOT CACHE RSS FEED
if (req.url ~ "/feed/") {
return ( pass );
}
## Do not cache search results, comment these 3 lines if you do want
to cache them
if (req.url ~ "/\?s\=") {
return ( pass );
}
# CLEAN UP THE ENCODING HEADER.
# SET TO GZIP, DEFLATE, OR REMOVE ENTIRELY. WITH VARY ACCEPT-ENCODING
# VARNISH WILL CREATE SEPARATE CACHES FOR EACH
# DO NOT ACCEPT-ENCODING IMAGES, ZIPPED FILES, AUDIO, ETC.
# ##########################################################
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
# 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 {
# unknown algorithm
remove req.http.Accept-Encoding;
}
}
# IF THIS IS A PURGE REQUEST, THEN CHECK THE IPS SET ABOVE
# BLOCK IF NOT ONE OF THOSE IPS
# ##########################################################
if (req.request == "PURGE") {
if ( !client.ip ~ purge ) {
error 405 "Not allowed.";
}
return (lookup);
}
# PIPE ALL NON-STANDARD REQUESTS
# ##########################################################
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
# ONLY CACHE GET AND HEAD REQUESTS
# ##########################################################
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# IF YOU GET HERE THEN THIS REQUEST SHOULD BE CACHED
# ##########################################################
return (lookup);
}
sub vcl_hash {
#this is to store cache based on PHPSESSID or woocommerce cookie so
cart doesn't show 0
if (req.http.cookie) {
hash_data(req.http.cookie);
}
#fix flexible ssl css
if (req.http.x-forwarded-proto) {
hash_data(req.http.x-forwarded-proto);
}
}
# HIT FUNCTION
# ##########################################################
sub vcl_hit {
# IF THIS IS A PURGE REQUEST THEN DO THE PURGE
# ##########################################################
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return (deliver);
}
# MISS FUNCTION
# ##########################################################
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return (fetch);
}
# FETCH FUNCTION
# ##########################################################
sub vcl_fetch {
# I SET THE VARY TO ACCEPT-ENCODING, THIS OVERRIDES W3TC
# TENDANCY TO SET VARY USER-AGENT. YOU MAY OR MAY NOT WANT
# TO DO THIS
# ##########################################################
set beresp.http.Vary = "Accept-Encoding";
}
# DELIVER FUNCTION #
##########################################################
sub vcl_deliver {
if ( req.url ~ "\?action=wpp_get_popular" && !req.http.cookie ~
"wordpress_logged_in" ) {
# Remove headers preventing Cloudflare cache
unset resp.http.Cache-Control;
unset resp.http.Expires;
unset resp.http.Pragma;
unset resp.http.ETag;
# Cache request in browser for 1 day (in seconds)
set resp.http.Cache-Control = "max-age=259200";
}
# IF THIS PAGE IS ALREADY CACHED THEN RETURN A 'HIT' TEXT
# IN THE HEADER (GREAT FOR DEBUGGING)
# ##########################################################
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
# IF THIS IS A MISS RETURN THAT IN THE HEADER
# ##########################################################
} else {
set resp.http.X-Cache = "MISS";
}
}
More information about the varnish-misc
mailing list