0

I'm having some troubles replacing a Apache2 reverse proxy rule for a Varnish ACL.

The apache rule is:

<Location /MySite>
    ProxyPass       http://192.168.0.123/OtherSite
</Location>

My Varnish ACL:

if (req.url ~ "^/(?i)MySite") {
    set req.backend = myhost; # 192.168.0.123
    set req.url = regsub(req.url, "^/(?i)MySite", "/OtherSite");
    return (pipe);
}

This is causing the URL on the client side to change to "/OtherSite", which I do not want to happen. On this scenario, when user enters http://www.myhost.com/MySite, the response is redirecting the user's browser to http://www.myhost.com/OtherSite. I want this "redirection" to happen only on varnish back-end request, having the same behavior as Apache's ProxyPass.

How can I achieve this?

Thank you

Renato Todorov
  • 233
  • 1
  • 10

1 Answers1

0

Please try this:

sub vcl_recv {
  ...
  if (req.url ~ "(?i)^/MySite") {
    set req.backend = myhost;
    set req.url = regsub(reg.url, "(?i)^/MySite", "/OtherSite");
  }
  ..
  return (lookup);
}
  1. Options for regular expressions must be entered right behind the opening quotes. See here: https://www.varnish-cache.org/docs/3.0/reference/vcl.html#regular-expressions
  2. Actually for Varnish req.url will start with "/" as this is the part of a URL following the port or (if no port given) the host part of an URL. Therefore you can replace the "/?" with "/".
  3. As long as you want to use Varnish as a reverse proxy (speaking of web acceleration by caching) you should avoid this "return (pipe);". This means stream back-end data but do not touch or cache it. Therefore I have removed this line. You see instead the default method "return (lookup);" to process the request in the reverse proxy cache. See here the flow-chart of Varnish: https://www.varnish-software.com/static/book/VCL_Basics.html#vcl-request-flow

Good luck a have fun with Varnish.

Jens Bradler
  • 6,503
  • 2
  • 17
  • 13