Search code examples
varnishhaproxyvarnish-vcl

Varnish Vcl for multiple incoming and outgoing port connections


Since a little while I am using Varnish Cache solutions and as long as the configuration was more or less the same as it was after the installation everything worked out very well.

But now I like to do a little bit more with Varnish. Currently I am using the following setup for my servers :

Visitors -> CloudFlare -> HaProxy -> Varnish (separate servers) -> Apache2 Content.

What I like to know is how am I able to make the right vcl script to accept let's say an incoming request from ip A on port B and redirect this to ip C on port D. (And this more than once.)

Example :

Default.Varnish works well like this:

DAEMON_OPTS="-a :8085,:8087 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,768m"

But now about the Varnish.Default :

backend default_1 { .host = "11.22.333.444"; .port = "8885"; }
backend default_2 { .host = "11.22.333.444"; .port = "8887"; }

And I tried something like this :

sub vcl_recv {
 if (server.port == 8885) { set req.backend = default_1; }
 if (server.port == 8887) { set req.backend = default_2; }
}

(Please be aware of the fact that both requests are going to the same outgoing server. Only the port is different!)

Someone who knows enough from Varnish already knows what I want I guess. I just like to use Varnish to proxy separate 'channels' based on different ports.


Solution

  • Try to check Varnish bind port, not backend port

    sub vcl_recv {
     if (server.port == 8085) { set req.backend = default_1; }
     if (server.port == 8087) { set req.backend = default_2; }
    }