Wednesday, 11 September 2013

varnish unable to cache https, 302 thrown

varnish unable to cache https, 302 thrown

I'm working on an app implemented in RoR. Varnish is used on top of it.
The problem with the varnish is that whenever there is an https session,
302 is thrown and varnish is unable to cache. Following is the vcl:
import std;
backend back1 {
.host = "localhost";
.port = "81";
.connect_timeout = 5s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 10s;
.probe = {
.url = "/";
.interval = 5s;
.timeout = 10s;
.window = 5;
.threshold = 3;
}
}
backend back2 {
.host = "localhost";
.port = "81";
.connect_timeout = 5s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 10s;
.probe = {
.url = "/";
.interval = 5s;
.timeout = 10s;
.window = 5;
.threshold = 3;
}
}
director voylla_ms round-robin {
{
.backend = back1;
}
{
.backend = back2;
}
}
sub vcl_recv {
set req.backend = app_ms;
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;
}
}
if (req.http.Cookie ~ "ap0ln") {
return(pass);
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.url ~ "^/account/") {
return (pass);
}
if ((req.url == "/") || (req.url ~ "^/t/") || (req.url ~ "^/designers/")
|| req.url ~ "^/taxons/" || (req.url ~
"\.(css|js|png|gif|jpeg|jpg|swf|html|ico|txt)$") || (req.url ~
"^/products/") || (req.url ~ "^/ql/") || (req.url ~ "^/search/")) {
std.syslog(180, "should be cached: " + req.http.host + req.url);
return (lookup);
} else {
std.syslog(180, "should not be cached: "+req.http.host+req.url);
return (pass);
}
if (req.http.Authorization) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
sub vcl_fetch {
std.syslog(180, "request cookie is" + req.http.Cookie);
if (req.http.Cookie ~ "ap0ln") {
return (deliver);
}
if ((req.url == "/") || (req.url ~ "^/t/") || (req.url ~ "^/designers/")
|| req.url ~ "^/taxons/" || (req.url ~
"\.(css|js|png|gif|jpeg|jpg|swf|html|ico|txt)$") || (req.url ~
"^/products/") || (req.url ~ "^/ql/") || (req.url ~ "^/search/")) {
std.syslog(180, "fetched: " + req.http.host + req.url);
if (req.url ~ "^/taxons/") {
set beresp.ttl = 3600s;
}
else {
set beresp.ttl = 86400s;
}
set beresp.http.X-Cacheable = "YES";
set beresp.http.Cache-Control = "public";
return (deliver);
}
else {
std.syslog(180, "a pass: " + req.http.host + req.url);
return (deliver);
}
}
sub vcl_error {
# std.syslog(180, "inside vcl_error");
return (deliver);
}
sub vcl_miss {
std.syslog(180, "inside vcl_miss");
return (fetch);
}
sub vcl_hit {
std.syslog(180, "inside vcl_hit");
return (deliver);
}
sub vcl_deliver {
std.syslog(180, "inside vcl_deliver");
std.syslog(180, "response cookie is " + resp.http.Cookie);
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver);
}
Could someone please point out why this happens and what is the best way
to fix it. Thanks in advance.

No comments:

Post a Comment