0

I am using varnish with magento2. however by default VCL doesn’t include device detection and assumes that websites are responsive so that the mobile and desktop browser would be served the same page. How do I change varnish config so that it saves cache based on user-agent?

0

You can try adding below changes to the subroutines in default.vcl

sub vcl_recv {

    if (req.http.User-Agent ~ "iP(?:hone|ad|od)|BlackBerry|Palm|Googlebot-Mobile|Mobile|mobile|mobi|Windows Mobile|Safari Mobile|Android|Opera (?:Mini|Mobi)") {
         set req.http.X-Device = "mobile";
    } else {
         set req.http.X-Device = "other";
    }

}

sub vcl_hash {

    if (req.http.X-Device) {
        hash_data(req.http.X-Device);
    }

}

sub vcl_deliver {

    set resp.http.X-Varnish-Device = req.http.X-Device;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.