我在apache前面使用varnish 4。我需要从首选语言es或ca(除非它也有de或en)的标头向deuts.de发出请求,以便重定向到spanish.es。有人能给我提供合适的语法吗?感谢
所以我设法在用于启动清漆的文件中放了一些东西:
sub vcl_recv {
if((req.http.Accept-Language !~ "de" || req.http.Accept-Language !~ "en") && (req.http.Accept-Language ~ "es" || req.http.Accept-Language ~ "ca" || req.http.Accept-Language ~ "eu"))
{
return(synth(301,"Moved Permanently"));
}
}
sub vcl_synth {
if(req.http.Accept-Language ~ "es" || req.http.Accept-Language ~ "ca" || req.http.Accept-Language ~ "eu")
{
set resp.http.Location = "http://spanish.es";
return (deliver);
}
}
这似乎工作
我用一些正则表达式略微扩展了所提出的解决方案,确保我们不会在accept语言标头中配置德语或英语作为优先级更高的语言
为了解释regex,我认为最好记住这样的Accept-Language
标头的样子:Accept-Language: de-DE,en-US,es
为了考虑用户的偏好,使用的regex会搜索所提供的语言,但同时确保以前找不到其他所提供的任何语言
后者通过负前瞻表达式"(^(?!de|en).)*"
在某种程度上神秘地实现,以确保de和en都不出现在"之前;es|ca|eu"条目。
^ # line beginning
.* # any character repeated any number of times, including 0
?! # negative look-ahead assertion
此外,我还添加了一个检查SSL是否已经用于在一个重定向中实现语言和SSL切换
使用return(synth(850, "Moved permanently"));
,您可以在vcl_synth中保存一个if子句,这将大大减少您的配置,尤其是当您必须执行许多基于语言的重定向时。
sub vcl_recv {
if (req.http.X-Forwarded-Proto !~ "(?i)https" && req.http.Accept-Language ~ "^((?!de|en).)*(es|ca|eu)" {
set req.http.x-redir = "https://spanish.es/" + req.url;
return(synth(850, "Moved permanently"));
}
}
sub vcl_synth {
if (resp.status == 850) {
set resp.http.Location = req.http.x-redir;
set resp.status = 301;
return (deliver);
}
}