Fixing backend URLs

Michael Alger varnish at mm.quex.org
Wed Sep 15 05:22:31 CEST 2010


On Tue, Sep 14, 2010 at 07:40:10PM -0400, Chris Cook wrote:
> I have an application that takes a URI that's submitted and spits back
> a different URI with a 302 redirect.  The backend server translates
> this and sends back a new URI that contains the backend server port.
> I want to use Varnish to remove the backend server port before sending
> the request to the client.
> 
> Varnish receives - http://domain.com:8080/blah/stuff
> Varnish makes it - http://domain.com/blah/stuff
> 
> What's the best way to do this?  I assume it involves vcl_fetch and
> some variants of the beresp, but I haven't been able to figure it out
> yet.

I think you want something like this in vcl_fetch():

  if (beresp.status == 301 || beresp.status == 302)
  {
    set beresp.http.Location = regsub(beresp.http.Location, "^(\w+://[^/]+):\d+", "\1");
  }

Seems to work okay in our UAT environment. The regex is hopefully
matching any protocol specification (\w+://) and any hostname([^/]+).
A colon after the hostname followed by any number of digits will
then get dropped, but the request path should remain intact.

Technically you can use a Location: header in other responses, so if
that matters to you, you might want to change the if's to just a
check if beresp.http.Location is present.




More information about the varnish-misc mailing list