Search code examples
varnishvarnish-4

Varnish host_header is not sended to backends


I trying to run varnish with two backends that needs exactly hostnames. But my nginx is receiving a localhost host header.

This is my configuration:

probe healthcheck {
    .url = "/";
    .interval = 5s;
    .timeout = 15s;
    .window = 5;
    .threshold = 3;
}

# Define the list of backends (web servers).
# Port 443 Backend Servers for SSL
backend bimer1 {
    .host = "nginx-proxy";
    .host_header = "site1.example.com.br";
    .port = "80";
    .probe = healthcheck;
}

backend bimer2 {
    .host = "nginx-proxy";
    .host_header = "site2.example.com.br";
    .port = "80";
    .probe = healthcheck;
}

This is my nginx access log:

bimer-cache-nginx-ssl-proxy_1 | 172.17.0.3 - - [21/Jun/2017:13:41:47 +0000] "POST /ws/Servicos/Geral/Localizacoes.svc/REST/LocalizarPessoas HTTP/1.1" 502 575 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36" <-> localhost 172.17.0.1, 172.17.0.3

It's look like set host_header parameter to backend is not working to regular request. But the healthcheck is working well.


Solution

  • Varnish is a transparent HTTP proxy. It will forward to backend whatever Host header was sent to it by the client (your browser). So if you have accessed it via http://localhost/, then localhost is what your backend will see in the Host header.

    Additionally, you should mostly never use DNS names in Varnish backend definitions. It should look like this instead:

    backend bimer1 {
        .host = "1.2.3.4";
        # ... etc.
    

    At present your configured backends resolve to the same machine nginx-proxy. Also the result for access.log is not from health checks. (the health checks that you have configured would show up as access to root URL / )

    Perhaps you have misunderstood Varnish configuration. If your plan was to serve multiple websites through same machine, then you should use only one backend for all. Multiple backends are for multiple machines.