安装Varish后,WordPress移动版本不再响应

时间:2020-03-11 作者:Jack Duldi

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
  # Set the X-Forwarded-For header so the backend can see the original
  # IP address. If one is already set by an upstream proxy, we\'ll just re-use that.

  #return(pass);

  if (req.method == "PURGE") {
     return (purge);
  }

  if (req.method == "XCGFULLBAN") {
     ban("req.http.host ~ .*");
     return (synth(200, "Full cache cleared"));
  }

  if (req.http.X-Requested-With == "XMLHttpRequest") {
     return(pass);
  }

  if (req.http.Authorization || req.method == "POST") {
     return (pass);
  }

  if (req.method != "GET" && req.method != "HEAD") {
     return (pass);
  }

  if (req.url ~ "(wp-admin|post\\.php|edit\\.php|wp-login|wp-json)") {
     return(pass);
  }

  if (req.url ~ "/wp-cron.php" || req.url ~ "preview=true") {
     return (pass);
  }
  if (req.url ~ "/xmlrpc.php" || req.url ~ "preview=true") {
     return (pass);
  }

  if ((req.http.host ~ "sitename.com" && req.url ~ "^some_specific_filename\\.(css|js)")) {
     return (pass);
  }

# Unset Cookies except for WordPress admin and WooCommerce pages
if (!(req.url ~ "(cart|my-account/*|wc-api*|checkout|addons|logout|lost-password|ask-question|product/*)")) {
unset req.http.cookie;
}
# Pass through the WooCommerce dynamic pages
if (req.url ~ "^/(cart|my-account/*|checkout|wc-api/*|addons|logout|lost-password|ask-question|product/*)") {
return (pass);
}
# Pass through the WooCommerce add to cart
if (req.url ~ "\\?add-to-cart=" ) {
return (pass);
}
# Pass through the WooCommerce API
if (req.url ~ "\\?wc-api=" ) {
return (pass);
}

  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\\s*)(_[_a-z]+|has_js)=[^;]*", "");

  # Remove the wp-settings-1 cookie
  set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

  # Remove the wp-settings-time-1 cookie
  set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

  # Remove the wp test cookie
  set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");

  # Remove the PHPSESSID in members area cookie
  set req.http.Cookie = regsuball(req.http.Cookie, "PHPSESSID=[^;]+(; )?", "");

  unset req.http.Cookie;
}

sub vcl_purge {
    set req.method = "GET";
    set req.http.X-Purger = "Purged";

    #return (synth(200, "Purged"));
    return (restart);
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.

    set beresp.grace = 12h;
    set beresp.ttl = 12h;
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}
这是我的默认设置。vcl配置。我的问题是我的网站在移动设备上不再有响应,它一直显示桌面版本。

有什么想法吗?

1 个回复
最合适的回答,由SO网友:Thijs Feryn 整理而成

WordPress完全有可能根据User-Agent 请求标题并相应地显示网站的正确版本。

如果不指示Varnish根据设备创建缓存变体,则每页只能缓存一个版本。

设备检测Varnish能够通过执行User-Agent 反省您可以包括以下VCL片段来加速此过程:https://github.com/varnishcache/varnish-cache/blob/master/etc/devicedetect.vcl.

为了触发设备检测子程序devicedetect.vcl 文件需要通过以下方式包括和执行call devicedetect 如下图所示:

include "devicedetect.vcl";
sub vcl_recv {
    call devicedetect;
}
VCL代码将生成自定义X-UA-Device 包含设备类型的请求标头。这将是:

pc-bot-mobile-bot-mobile-iphone-mobile-firefoxos-mobile-smartphone-mobile-General-mobile-smartphone-mobile-android-ipad-tablet-android-hp-tablet-hp-microsoft-tablet-kindle-Cache变体/h1>基于X-UA-Device 标头,您可以通过修改sub vcl_hash 逻辑:

sub vcl_hash {
  if(req.http.X-UA-Device ~ "(mobile|tablet)") {
    hash_data("mobile");
  }
}
此VCL片段确保移动设备和平板电脑的缓存对象有第二个版本。

相关推荐

Wp_cache_set不工作有什么原因吗?

wp\\u cache\\u设置不工作的原因是什么?我一直在转动车轮,试图弄明白为什么这些都不起作用。有什么建议吗?这些函数旨在帮助我将结果缓存到键/对象,然后利用键/对象显示信息。但是,它们不存储密钥/对象$related_post_ids = wp_cache_get( \'related_post_ids\' ); if ( false === $related_post_ids ) { //seting args and run query $the_query = new