将域名从nginx作为前端传递给varnish作为后端

时间:2014-05-11 14:09:48

标签: apache nginx varnish

更新:解决了!检查我对这个问题的回答。

我有一个nginx服务器作为前端,使用varnish作为他的后端,使用apache2作为后端的varnish。类似的东西:

nginx的:

Nginx配置为侦听*:80

location / {
    proxy_pass   http://127.0.0.1:81;
}

清漆:

Varnish配置为侦听127:0.0.1:81

backend 1 {
        .host = "127.0.0.1";
        .port = "8081";

        ...
}

backend 2 {
        .host = "127.0.0.1";
        .port = "8080";

        ...
}

sub vcl_recv {

    if (req.http.host == <1 domain>) {
        # setting the backend to 1
        set req.backend = 1;
    } else {
        # setting the backend to 2
        set req.backend = 2;
    }

    ...

}

apache2的:

apache配置为侦听127.0.0.1:8080和127.0.0.1:8081

// ports.conf

VirtualHost 127.0.0.1:8080
Listen 8080

VirtualHost 127.0.0.1:8081
Listen 8081

// other vhost confs

<VirtualHost 127.0.0.1:8080>
...
</VirtualHost>

<VirtualHost 127.0.0.1:8081>
...
</VirtualHost>

如何将HTTP主机(如example.com或secure.example.com)从nginx传递到varnish,以识别所请求的站点?

1 个答案:

答案 0 :(得分:1)

这是我的解决方案,对我有用。其他答案也将受到欢迎!

只需将proxy_set_header Host www.example.com;添加到nginx中的location / {

这样的事情:

server {

    server_name example.com www.example.com;

    location / {
            proxy_pass   http://127.0.0.1:8080;

            # THIS line was added:
            proxy_set_header Host www.example.com;
    }

    ...

}