简体   繁体   中英

Redirect HTTPS on multidomain Varnish

i have got two domain based on same framework (magento2) domain1.it domain2.com

I would like to redirect them to their respective SSL version. https://domain1.it https://domain2.com

Domain 1 is correctly configured to redirect to HTTPS and my varnish Config file is:

sub vcl_recv {
if ( (req.http.host ~ "^(?i)www.domain1.it" || req.http.host ~ "^(?i)domain1.it") && req.http.X-Forwarded-Proto !~ "(?i)https") {
return (synth(750, ""));
    }

sub vcl_synth {
if (resp.status == 750) {
    set resp.status = 301;
    set resp.http.Location = "https://domain1.it" + req.url;
return(deliver);
}

the problem is the synth always redirect to the same domain.

I should add an if condition where i could call a subroutines that redirect to https for domain2

For the love of everything that is good, please stop using otherworldly status codes, 301 and 302 are perfectly fine, clearer and save you a line.

I would advise against using x-forwarded-proto and use an SSL/TLS terminator that supports the PROXY protocol, but since this is what you have, here you go:

sub vcl_recv {
    if (req.http.X-Forwarded-Proto !~ "https") {
        set req.http.location = "https://" + req.http.host + req.url;
        return(synth(301));
    }
}

sub vcl_synth {
    if (resp.status == 301 || resp.status == 302) {
        set resp.http.location = req.http.location;
        return (deliver);
    }
}

relevant link: https://info.varnish-software.com/blog/rewriting-urls-with-varnish-redirection

Bitnami Engineer here. I just reviewed the Varnish documentation and found this:

sub vcl_recv {
    if (client.ip != "127.0.0.1" && std.port(server.ip) == 80 && req.http.host ~ "^(?i)example.com") {
        set req.http.x-redir = "https://" + req.http.host + 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 = 302;
        return (deliver);
    }
}

This is useful when you want to redirect the clients to an SSL-version of your site. More info here:

https://varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM