smartDonkey
smartDonkey

Reputation: 550

Pass the domain from nginx as frontend to varnish as backend

Update: solved! check my answer to this question.

I have an nginx server as frontend, which using varnish as his backend, and varnish using apache2 as his the backend. something like:

nginx:

Nginx is configured to listen on *:80

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

varnish:

Varnish is configured to listen on 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 is configured to listen on 127.0.0.1:8080 and on 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>

How to pass the HTTP host (like example.com or secure.example.com) from nginx to varnish, to identify the site requested?

Upvotes: 1

Views: 634

Answers (1)

smartDonkey
smartDonkey

Reputation: 550

This is my solution, that worked for me. other answers will be welcome too!

Just add proxy_set_header Host www.example.com; to location / { in nginx.

Something like this:

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;
    }

    ...

}

Upvotes: 1

Related Questions