简体   繁体   English

Varnish 如何为移动和桌面站点缓存

[英]Varnish how to cache for mobile and desktop site

I want to know how to cache for mobile and desktop site.我想知道如何缓存移动和桌面网站。 I have mobile and desktop site whose root is written in nginx. Mobile/Desktop is served to the user based on the user-agent whenever the user visits the site, so in this scenario how to cache for mobile and cache for desktop site so that when the user visits the website, Get the right content from the cache.我有移动和桌面站点,其根目录写在 nginx 中。每当用户访问该站点时,移动/桌面都会根据用户代理向用户提供服务,因此在这种情况下,如何为移动站点缓存和为桌面站点缓存,以便当用户访问网站时,从缓存中获取正确的内容。

Please help to write VCL for mobile and desktop cache in Varnish.请帮助在 Varnish 中编写用于移动和桌面缓存的 VCL。

You can download https://github.com/varnishcache/varnish-devicedetect/blob/master/devicedetect.vcl and include this file in your main VCL file.您可以下载https://github.com/varnishcache/varnish-devicedetect/blob/master/devicedetect.vcl并将此文件包含在您的主 VCL 文件中。 By calling the devicedetect subroutine in your main VCL file.通过调用主 VCL 文件中的devicedetect子例程。

This subroutine will set a X-UA-Device header that contains the device type, which you can then vary on.此子例程将设置一个包含设备类型的X-UA-Device header,然后您可以启用它。

Here's an example:这是一个例子:

vcl 4.1;

backend default {
    .port = "8080";
}

include "devicedetect.vcl";

sub vcl_recv {
    call devicedetect;
    if(req.http.X-UA-Device ~ "^(mobile|tablet)\-.+$") {
        set req.http.X-UA-Device = "mobile";
    } else {
        set req.http.X-UA-Device = "desktop";
    }
}

sub vcl_hash {
    hash_data(req.http.X-UA-Device);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM