这是我在阅读和参考了大量资料后构建的 default.vcl,varnish 配置文件:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
acl purge {
"localhost";
}
# Called when a request is received
sub vcl_recv {
if (req.request == "BAN") {
if(!client.ip ~ purge) {
error 405 "Not allowed.";
}
ban("req.url ~ "+req.url+" && req.http.host == "+req.http.host);
error 200 "Banned.";
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
#Requests for login, admin, sign up, preview, password protected posts, admin-ajax or other ajax requests
if (req.url ~ "(wp-login|wp-admin|wp-signup|preview=true|admin-ajax.php)" || req.http.Cookie ~ "(wp-postpass|wordpress_logged_in|comment_author_)" || req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)") {
return (pass);
}
remove req.http.cookie;
return (lookup);
}
# Called after a document has been successfully retrieved from the backend
sub vcl_fetch {
if (beresp.status == 404 || beresp.status == 503 || beresp.status >= 500) {
set beresp.ttl = 0m;
return(hit_for_pass);
}
# Requests for login, admin, sign up, preview, password protected posts, admin-ajax or other ajax requests
if (req.url ~ "(wp-login|wp-admin|wp-signup|preview=true|admin-ajax.php)" || req.http.Cookie ~ "(wp-postpass|wordpress_logged_in|comment_author_)" || req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)") {
return (hit_for_pass);
}
# Don't cache .xml files (e.g. sitemap)
if (req.url ~ "\.(xml)$") {
set beresp.ttl = 0m;
}
# Cache HTML
# if (req.url ~ "\.(html|htm)$") {
# set beresp.ttl = 60m;
# }
remove beresp.http.set-cookie;
set beresp.ttl = 24h;
return (deliver);
}
保存文件后,当我尝试重新启动 Varnish 时,发生了以下情况:
user@host:~$ sudo service varnish restart
* Stopping HTTP accelerator varnishd [fail]
* Starting HTTP accelerator varnishd [fail]
Message from VCC-compiler:
Syntax error at
('input' Line 62 Pos 19)
set beresp.ttl = 0m;
------------------#---------
Running VCC-compiler failed, exit 1
VCL compilation failed
这实际上意味着我的 default.vcl 中的这一行有问题:
# Don't cache .xml files (e.g. sitemap)
if (req.url ~ "\.(xml)$") {
set beresp.ttl = 0m;
}
但我不确定这是什么。我做错了什么?
答案1
错误是由于配置(default.vcl)中从位置 19 开始的第 62 行有多余的空格造成的,正如错误中清楚指出的那样:
set beresp.ttl = 0m;
------------------#---------
笔记:表示#
位置。