Varnish在URL中添加更多斜杠

时间:2018-04-07 07:05:41

标签: varnish

我的清漆缓存是加速器模式,它会导致在URL之后添加更多斜杠。 我查看了gtmetrix:

Avoid landing page redirects for the following chain of redirected URLs.
https://www.example2.com/
https://www.example2.com//
https://www.example2.com/

并使用google pagespeed测试: 减少服务器响应时间 在我们的测试中,您的服务器在1.6秒内响应。有许多因素会降低服务器响应时间。

这是我的配置:

vcl 4.0;

backend default {
.host = "localhost";
.port = "8080";
}

acl purger {
"localhost";
"XXX.XXX.XXX.XXX";
}

sub vcl_recv {

 if (client.ip != "127.0.0.1" && req.http.host ~ "XXX.com") {
 set req.http.x-redir = "https://www.example.com/" + req.url;
 return(synth(850, ""));
 }

 if (client.ip != "127.0.0.1" && req.http.host ~ "YYY.com") {
 set req.http.x-redir = "https://www.example2.com/" + req.url;
 return(synth(850, ""));
 }
 if (req.method == "PURGE") {
    if (!client.ip ~ purger) {
    return(synth(405, "This IP is not allowed to send PURGE requests."));
  }
 return (purge);
 }

 if (req.restarts == 0) {
 if (req.http.X-Forwarded-For) {
 set req.http.X-Forwarded-For = client.ip;
 }
 }

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

 if (req.url ~ "/feed") {
 return (pass);
 }

 set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", "");
 set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", "");

 if (req.http.cookie == "") {
 unset req.http.cookie;
 }
}

sub vcl_synth {
 if (resp.status == 850) {
 set resp.http.Location = req.http.x-redir;
 set resp.status = 302;
 return (deliver);
 }
}

sub vcl_purge {
set req.method = "GET";
 set req.http.X-Purger = "Purged";
 return (restart);
 }

sub vcl_backend_response {
 set beresp.ttl = 24h;
 set beresp.grace = 1h;

}

sub vcl_deliver {
 if (req.http.X-Purger) {
 set resp.http.X-Purger = req.http.X-Purger;
}
}

有人知道清漆这样做的原因吗?

1 个答案:

答案 0 :(得分:0)

req.url始终包含/

所以正确的是:

set req.http.x-redir = "https://www.example2.com" + req.url;

您的问题与example1 / example2不对应,因为它包含XXX和YYY

如果你可以发布一个较少编辑的VCL - 我可以帮你多一点 在重定向之前,您可能需要!= "/" req.url。因为这会产生//