regsub in error response

Caunter, Stefan scaunter at topscms.com
Tue Sep 14 20:10:41 CEST 2010


My condition is based on a check of req.url.

  #mobile rules
                if(! req.url ~ "(^/iphone|^/mobile|^/rss)" &&
req.http.user-agent ~ "([B
b]lack[Bb]erry|[iI][eE][mM]obile)") {
                        set req.url = regsub(req.url, "(.*)",
"/mobile\1");
                        error 750 "Moved Temporarily";
                }

The req.http.Host header will always be present in HTTP/1.1 requests.

Also, you're running 2.06 and I'm running 2.1.3 (which shouldn't matter
although I haven't looked at the changelog).

Stefan


-----Original Message-----
From: Chris Hecker [mailto:checker at d6.com] 
Sent: September-14-10 2:06 PM
To: Caunter, Stefan
Cc: varnish-misc at varnish-cache.org
Subject: Re: regsub in error response


Okay, figured this out.

in vcl_recv I check req.http.Host, and if it's wrong, I set it correctly

and throw 751, then in vcl_error:

         if(obj.status == 751) {
                 # redirect
                 set obj.http.Location = "http://" req.http.Host
req.url;
                 set obj.status = 301;

This seems to work.  Can anybody see any problems?  Is it bad to set 
req.http.Host?  Will that string concatenation above always be well
formed?

Thanks,
Chris


On 2010/09/14 10:56, Chris Hecker wrote:
>
> But the req.url doesn't seem to have the domain name in it, which is
> what I want to redirect...
>
> Chris
>
>
>
> On 2010/09/14 10:46, Caunter, Stefan wrote:
>> You need to tell the browser the new location in vcl_error. I have
>> something similar for mobile redirects.
>>
>> First in vcl_recv catch the event:
>>
>> if( ... some condition ... ) {
>> set req.url = regsub(req.url, "(.*)",
>> "/mobile\1");
>> error 750 "Moved Temporarily";
>> }
>>
>> Then handle it in vcl_error:
>>
>> if (obj.status == 750) {
>> set obj.http.Location = req.url;
>> set obj.status = 302;
>> return(deliver);
>> }
>>
>> You can specify obj.status = 301 of course.
>>
>> Stefan Caunter :: Senior Systems Administrator :: TOPS
>> e: scaunter at topscms.com :: m: (416) 561-4871
>> www.thestar.com www.topscms.com
>>
>>
>> -----Original Message-----
>> From: varnish-misc-bounces at varnish-cache.org
>> [mailto:varnish-misc-bounces at varnish-cache.org] On Behalf Of Michael
>> Alger
>> Sent: September-11-10 5:52 AM
>> To: varnish-misc at varnish-cache.org
>> Subject: Re: regsub in error response
>>
>> On Fri, Sep 10, 2010 at 10:12:59AM -0700, Chris Hecker wrote:
>>>
>>> I'd like to do a 301 from a.com to b.com, but preserving the whole
>>> url, but I can't figure out how to do it. This doesn't work to
>>> send it to vcl_error:
>>>
>>> error 751 regsub(req.url,"a.com","b.com");
>>>
>>> The regsub isn't allowed there. Can I stuff it in a header that I
>>> can get in vcl_error?
>>
>> You probably can. What I do for redirects is to rewrite the req.url
>> and then use that in the error statement, i.e.
>>
>> set req.url = regsub (req.url, "rewritefrom", "rewriteto");
>> error 751 req.url;
>>
>> For your particular case where you don't actually want to rewrite
>> the URL but instead only change the host part of it, I would do
>> something like:
>>
>> if (req.url ~ "(?i)^/vanitypath")
>> {
>> set req.url = "http://b.com" req.url;
>> error 751 req.url;
>> }
>>
>> Note that the host isn't present in req.url (use req.http.Host for
>> that) - so in the first example, "rewriteto" should start with a
>> full protocol specification (http://...").
>>
>> Most browsers work fine if they're told to redirect to /foo, but I
>> don't really like relying on it.
>>
>> _______________________________________________
>> varnish-misc mailing list
>> varnish-misc at varnish-cache.org
>> http://lists.varnish-cache.org/mailman/listinfo/varnish-misc
>>
>> _______________________________________________
>> 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