Fixing backend URLs

Chris Cook crcook at gmail.com
Wed Sep 15 06:04:32 CEST 2010


Awesome - the regex works perfectly.  I think I'll keep it with the redirect since those were the only area I was observing a suboptimal URI. Thanks!

-Chris

On Sep 14, 2010, at 11:22 PM, Michael Alger wrote:

> 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.
> 
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at varnish-cache.org
> http://lists.varnish-cache.org/mailman/listinfo/varnish-misc





More information about the varnish-misc mailing list