如何在 Varnish 中将所有域名重定向到 https?

如何在 Varnish 中将所有域名重定向到 https?

我在用着

Apache on port 8080
Nginx on 443
Varnish on 80

我不想每次向服务器添加新域时都更改我的 varnish 配置。我想知道是否有任何正则表达式或变量,以便 varnish 将所有域重定向到 https。

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

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

我有

example.com abc.com xyz.com 123.com www.420.com abc.123.com

我希望所有子域名(以及所有未来的域名,如果可能的话)都重定向到 https。

谢谢。

答案1

尝试以下配置

sub vcl_recv {

if (req.http.host == "www.example.com" &&
    req.url ~ "^/admin")
{

  if (req.http.X-Forwarded-Proto !~ "https") {
    return (synth(850, "Moved Permanently"));
  }

return (pass);
}
    if (req.restarts == 0) {
    if (req.http.x-forwarded-for) {
        set req.http.X-Forwarded-For =
        req.http.X-Forwarded-For + ", " + client.ip;
    } else {
        set req.http.X-Forwarded-For = client.ip;
    }
}


sub vcl_synth {
  if(resp.status == 850) {
    set resp.http.Location = "https://" + req.http.host + req.url;
    set resp.status = 301;
}

相关内容