Ankit
Ankit

Reputation: 41

Varnish with Magento 1.8.1 Community editon not getting cache results

I'm newbie to Varnish Cache. I've installed Varnish 4 ( latest version) on Cent OS server successfully and configured perfectly. I'm also getting X-Varnish, Via 1.1 varnish - v4 in Response header. I've found everything working perfectly. If I stop varnish, website stops which seems correct.

My question is, eventhough varnish is configured correctly I'm not having enough speed. I thought, I'm not getting varnish cache result, it looks server always called backend server to get results. Normally, without varnish it takes 3-4 seconds to get results. After installing varnish, It takes same amount of time.

Resonse header

Accept-Ranges bytes Age 0 Cache-Controlno-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connectionkeep-alive Content-EncodinggzipContent-Typetext/html; charset=UTF-8DateFri, 01 Aug 2014 14:08:51 GMTExpiresThu, 19 Nov 1981 08:52:00 GMTPragmano-cacheServerApache/2.2.15 (CentOS)Set-Cookiefrontend=rfdi8hd6kq136puafk93lm0ra7; expires=Fri, 01-Aug-2014 15:08:51 GMT; path=/; domain=www.usapooldirect.com; httponlyTransfer-EncodingchunkedVaryAccept-Encoding,User-Agent Via 1.1 varnish-v4 X-Powered-By PHP/5.3.3X-Varnish

Request Header

Accepttext/html, application/xhtml+xml, application/xml;q=0.9 ,/;q=0.8Accept-Encodinggzip, deflateAccept-Languageen-US,en;q=0.5Connectionkeep-aliveCookiefrontend=rfdi8hd6kq136puafk93lm0ra7; external_no_cache=1; adminhtml=pt3bt6t30m4vtqdsm1ldv716v7Hostwww.usapooldirect.com Refererhttp://www.usapooldirect.com/User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0

Thank you,

Ankit

Upvotes: 2

Views: 1046

Answers (3)

Waseem Abbas
Waseem Abbas

Reputation: 90

Whatever module you'll use to improve caching, as long as Magento is involved - even if it is only for determining what data to load from its cache - it will be slow. Varnish is always been a solution to speed up Magento.

But after Magento 1.8.1, Varnish cache is practically becomes useless. You can see here how you can configure Varnish Cache with Magento 1.8.1! and above.

Upvotes: 2

KNOWARTH
KNOWARTH

Reputation: 922

You can use below sample Varnish file.

backend default {
.host = "localhost";
.port = "80";
}

sub vcl_recv {
if (req.http.x-forwarded-for) {
remove 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;
}

if (!req.backend.healthy) {
unset req.http.Cookie;
}

if (req.backend.healthy) {
set req.grace = 60s;
} else {
set req.grace = 24h;
}

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 == "POST") {
return(pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}

if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
remove req.http.Accept-Encoding;
}
elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
}
elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
set req.http.Accept-Encoding = "deflate";
}
}       else {
remove req.http.Accept-Encoding;
}

if (!req.backend.healthy) {
unset req.http.Cookie;
}
if (req.http.Cookie) {
return (pass);
}
return (lookup);
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (pass);
}

sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}

sub vcl_hit {
return (deliver);
}

sub vcl_miss {
return (fetch);
}

sub vcl_fetch {
if (beresp.status == 404 || beresp.status == 503) {
set beresp.ttl = 0s;
return (hit_for_pass);
}
if (!req.backend.healthy) {
set beresp.ttl = 1h;
unset beresp.http.set-Cookie;
}
if (beresp.http.Set-Cookie) {
return (hit_for_pass);
}
set beresp.grace = 24h;
return (deliver);
}

sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Varnish-Cache = "HIT";
}
else {
set resp.http.X-Varnish-Cache = "MISS";
}
}

sub vcl_init {
return (ok);
}

sub vcl_fini {
return (ok);
}

sub vcl_error {
return(deliver);
}

Upvotes: 1

KNOWARTH
KNOWARTH

Reputation: 922

You should check Varnish hits with help of Varnishstat.

You can also put below code which you can check in browser if it got served from Varnish cache or backend.

sub vcl_deliver {
if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT ("+obj.hits+")";
} else {
    set resp.http.X-Cache = "MISS";
    #    set resp.http.X-Cache-Hash = obj.http.hash;
}
return (deliver);
}

Upvotes: 2

Related Questions