# 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配置。我的问题是我的网站在移动设备上不再有响应,它一直显示桌面版本。
有什么想法吗?
最合适的回答,由SO网友:Thijs Feryn 整理而成
WordPress完全有可能根据User-Agent
请求标题并相应地显示网站的正确版本。
如果不指示Varnish根据设备创建缓存变体,则每页只能缓存一个版本。
为了触发设备检测子程序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片段确保移动设备和平板电脑的缓存对象有第二个版本。