Fixing backend URLs

Dang Tung tungcnw at gmail.com
Fri Sep 24 06:21:18 CEST 2010


Dear,

It's not work for me, may be my configurations are wrong, could anyone help
me please. Here's my configuration:

sub vcl_recv {

    # Host header check.
    # * Check if we recognize the domain used
    # * Normalize host headers to limit cache usage
    # * Rewrite URL for the virtual host monster
    # Return an error for other requests.


    # cache multimedia
    #if (req.request == "GET" && req.url ~ "\.(jpg|jpeg|gif|ico)$") {
    #  lookup;
    #}

    # cache CSS and JS files
    if (req.request == "GET" && req.url ~ "\.(css|js)$") {
    lookup;
    }

    # do not cache POST requests
    if (req.request == "POST") {
        pipe;
    }

    if (req.request != "GET" && req.request != "HEAD") {

    # figure out if you are allowed to purge stuff from cache
        if (req.request == "PURGE") {
            if (!client.ip ~ purge) {
                error 405 "Not allowed.";
            }
            lookup;
        }

    # else just pass it along
    pipe;
    }

    if (req.http.Authenticate || req.http.Authorization) {
        pass;
    }
}

sub vcl_hit {
  if (req.request == "PURGE") {
    set obj.ttl = 0s;
    error 200 "Purged.";
  }
}

sub vcl_miss {
  if (req.http.If-Modified-Since) {
    pass;
  }

  if (req.request == "PURGE") {
    error 404 "Not in cache.";
  }
}

sub vcl_fetch {
     if (!beresp.cacheable) {
         return (pass);
     }
     if (beresp.http.Set-Cookie) {
         return (pass);
     }
     return (deliver);
 if (beresp.status == 301 || beresp.status == 302)
 {
   set beresp.http.Location = regsub(beresp.http.Location,
"^(\w+://[^/]+):\d+", "\1");
 }
}

Many thanks and regards.

On Wed, Sep 15, 2010 at 11:04 AM, Chris Cook <crcook at gmail.com> wrote:

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



-- 
Nguyen Dang Tung
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20100924/eefd7925/attachment-0003.html>


More information about the varnish-misc mailing list