Question

drupal_goto() uses a 302 redirect by default, which means a lot of redirects, including form redirects, get served with a 302 response code.

Having a issue in Varnish that is caching 302 response from user submitted forms for anonymous traffic.

It caches the first 302 redirect but then if the customer selects different options on the form it should be redirected to another url.

Not being able to cache product pages on our Drupal commerce site amounts to millions of cache misses a month and excessive performance issues. Drupal based page level caching is not a option as we have over 4,500 stores deployed.

How can this be fixed?

Was it helpful?

Solution

Disable caching in fetch if response status is 302

sub vcl_fetch {
set beresp.grace = 24h;

if (beresp.status >= 400) {
     return (hit_for_pass);
}

// New Set Longer Cache
if (req.http.user-agent ~ "(Googlebot|msnbot|Yandex|Slurp|Bot|Crawl|bot|Baid|Mediapartners-Google)") {
    unset beresp.http.set-cookie;
    set beresp.ttl = 5d;
    return (deliver);
}
if (req.request == "GET" && req.url ~ "\.(css|xml|txt)$") {
    set beresp.ttl = 5d;
    unset beresp.http.set-cookie;
    return (deliver);
}
// multimedia
if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|tga|woff|eot|ttf|svg|wmf|js|swf|ico)$") {
    unset beresp.http.set-cookie;
    set beresp.ttl = 5d;
    return (deliver);
}
//Fix to stop caching 302 responses
if (beresp.status == 302) {
  remove beresp.http.Cache-Control;
  set beresp.http.Cache-Control = "private, no-cache";
  set beresp.ttl = 0s;
}
set beresp.ttl = 5d;
return (deliver);

}

Setting cache control for 302 redirect:

sub vcl_deliver {
// Remove Varnish identifying information from headers
// Note: Both cache hits and cache misses will use this subroutine.

set resp.http.Access-Control-Allow-Origin = req.http.X-Saved-Origin;

remove resp.http.Via;
unset resp.http.Server;
remove resp.http.Age;
remove resp.http.X-Powered-By;
remove resp.http.X-Varnish;
remove resp.http.X-Generator;
remove resp.http.X-Mod-Pagespeed;
remove resp.http.X-Drupal-Cache;
remove resp.http.Link;
set resp.http.Server = "Prism";
// Stop Caching 302 redirects
if (resp.status == 302) {
  remove resp.http.Cache-Control;
  set resp.http.Cache-Control = "private, no-cache";
}

if(req.http.host ~ "printsites.net"){
     set resp.http.X-Robots-Tag = "noindex, nofollow";
}

return(deliver);

}

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top