配置Varnish 4个多个域的不同端口

时间:2018-11-11 02:26:58

标签: varnish

我可以同时加载两个网站,但是无法登录两个wordpress网站。

backend websiteone {
    .host = "127.0.0.1";
    .port = "7070";
}
backend websitetwo {
    .host = "127.0.0.1";
    .port = "2082";
}
    sub vcl_recv {
    if (req.http.host ~ "^(.*\.)?websiteone\.com$") {
    set req.backend_hint = websiteone;
    return (hash);
}
    if (req.http.host ~ "^(.*\.)?websitetwo\.com$") {
    set req.backend_hint = websitetwo;
    return (hash);
    }
}

1 个答案:

答案 0 :(得分:0)

这是我为解决此问题所做的事情:

mkdir /etc/varnish/sites-enabled

cd /etc/varnish/sites-enabled

nano siteone.com.vcl

sub vcl_recv {
    if (req.http.host == "siteone.com") {
        if (req.url ~ "/(cart|my-account|checkout|addons|/?add-to-cart=)") {
        return (pass);
        }
    }
}

nano sitetwo.com.vcl

backend sitetwo {
    .host = "127.0.0.1";
    .port = "2082";
}

sub vcl_recv {
       if (req.http.host == "sitetwo.com") {
           set req.backend_hint = sitetwo;
    }
}

然后我必须编辑/etc/varnish/default.vcl

cd /etc/varnish/

nano default.vcl

如上所示,后端是为sitetwo配置的,但是siteone的后端是在default.vcl中配置的,所以我更改了

backend default {
    .host = "127.0.0.1";
    .port = "7070";
}

并将这两行添加到default.vcl的底部

 include "sites-enabled/siteone.com.vcl";
 include "sites-enabled/sitetwo.com.vcl";

现在一切似乎都能正常工作!而且,如果我需要添加更多网站,我要做的就是在/ sites-enabled文件夹中创建sitetree.com.vcl并粘贴

backend sitethree {
    .host = "127.0.0.1";
    .port = "port number";
}

sub vcl_recv {
       if (req.http.host == "sitethree.com") {
           set req.backend_hint = sitethree;
    }
}

注意: Siteone正在运行一个电子商务网站,因此,如果您未在运行商店,则应该能够在siteone.com.vcl

中使用以下内容
sub vcl_recv {
       if (req.http.host == "siteone.com") {
           set req.backend_hint = siteone;
    }
}