Varnish 正在为 google 移动测试人员呈现桌面网站

Varnish is rendering desktop site for google mobile tester

我使用 Varnish 来缓存我网站的移动和桌面版本,然后使用 https://github.com/varnishcache/varnish-devicedetect. But when testing the site with https://www.google.com/webmasters/tools/mobile-friendly/ 根据用户代理字符串显示正确的版本我得到了桌面网站。分叉 varnish-devicedetect 并添加用户代理 google 在访问该站点时使用是否有意义?或者有其他更好的解决方案吗?

我知道如果网站能够响应,那将不是问题,但现在还不行。

使用这个:

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