Varnish正在为谷歌移动测试仪呈现桌面版网站

时间:2016-06-02 10:34:57

标签: mobile varnish

我正在使用Varnish缓存我的网站的移动版和桌面版,然后使用https://github.com/varnishcache/varnish-devicedetect根据用户代理字符串显示正确版本。但是当使用https://www.google.com/webmasters/tools/mobile-friendly/测试网站时,我会获得桌面网站。在访问网站时分叉清漆设备检测并添加用户代理谷歌使用是否有意义?或者是否有其他解决方案可以更好地运作?

我知道如果该网站有响应但这不是一个问题,但现在不是一个选择。

1 个答案:

答案 0 :(得分:1)

使用这个:

(来自https://github.com/varnishcache/varnish-devicedetect的子集)

sub detectbot {
  unset req.http.X-Bot-Detected;

    # mobile bots
    if (req.http.User-Agent ~ "\(compatible; Googlebot-Mobile/2.1; \+http://www.google.com/bot.html\)"
      || (req.http.User-Agent ~ "iPhone" && req.http.User-Agent ~ "\(compatible; Googlebot/2.1; \+http://www.google.com/bot.html")) {
        set req.http.X-Bot-Detected = "mobile-bot";
    }
    # other bots
    elsif (req.http.User-Agent ~ "(?i)(ads|google|bing|msn|yandex|baidu|ro|career|seznam|)bot"
      || req.http.User-Agent ~ "(?i)(baidu|jike|symantec)spider"
      || req.http.User-Agent ~ "(?i)scanner"
      || req.http.User-Agent ~ "(?i)(web)crawler") {
        set req.http.X-Bot-Detected = "bot";
    }
}

将其保存为detect-bot.vcl(与varnish default.vcl在同一目录中) 然后在default.vcl的顶部

include "detect-bot.vcl";

然后在.vcl

中添加以下部分
backend mobile {
    .host = "10.0.0.1";
    .port = "80";
}

sub vcl_recv {
    # add a header "X-Bot-Detected" when this request was done by a bot
    call detectbot;
}

sub vcl_recv {
    # call some detection engine
    if (req.http.X-UA-Device ~ "^mobile-bot" ) {
        set req.backend = mobile;
    }
}
sub vcl_hash {
    if (req.http.X-UA-Device) {
        hash_data(req.http.X-UA-Device);
    }
}

此示例将请求发送到另一个后端。根据设置的工作原理,您需要调整最后一部分。有关更多示例,请参阅https://www.varnish-cache.org/docs/4.1/users-guide/devicedetection.html

相关问题