URL rewrite with Varnish

Michael Alger varnish at mm.quex.org
Tue Apr 3 16:01:55 CEST 2012


On Tue, Apr 03, 2012 at 03:35:31PM +0200, Rafał Zawierta wrote:
> W dniu 3 kwietnia 2012 15:25 użytkownik AD <straightflush at gmail.com>napisał:
> 
> > On Tue, Apr 3, 2012 at 9:07 AM, James Light <j.gareth.light at gmail.com>wrote:
> >
> >> On Apr 3, 2012 8:53 AM, "Rafał Zawierta" <zawierta at gmail.com> wrote:
> >> >
> >> > Hello,
> >> >
> >> > Is it possible to handle such case: my webapp is running on
> >> > http://10.0.0.10:8888/content/site/EN.html and whole site is on base
> >> > url: http://10.0.0.10:8888/content/site/.
> >> >
> >> > I want to make my site available via Varnish on url:
> >> > http://mysite.mydomain.com/ - I want to remove whole stuff after
> >> > / from url.
> >> >
> >> > Rule:
> >> > sub vcl_recv {
> >> > if (req.http.host ~ "^(www\.)?mysite\.mydomain\.com$" ) {
> >> >   set req.url = regsub(req.url, "^/content/site/", "/");
> >> > }
> >> > }
> >> >
> >> > isn't working at all.
> >> >
> >> > Regards
> >> > R.
> >>
> 
> Sorry AD, but I'm not sure if your tip is helpful.
> Once again: my backend server has ip 10.0.0.10, port 8888. It runs multiple
> apps, so if I type http://10.0.0.10:8888 i get default site
> http://10.0.0.10:8888/content/default/EN.html. Therefore I'd like varnish
> to point me to /content/site/EN.html AND to remove "content/site/" from
> URL.
> That's why passing http.host won't work at all.

I think your original example is doing the reverse of what you want. You
want to ADD /content/site/ to the incoming request from the client,
rather than remove it.

So, client requests / or /EN.html from your server; you want to do
something like

  set req.url = "/content/site" + req.url;
  # (not entirely sure of the syntax here... I think in 2.1 you can just
  # put  set req.url = "prefix" req.url;  or you can use a regsub with
  # "^" as the pattern to replace)

in your VCL in order to prepend "/content/site" to the request the
client sent, so that when it's sent to the backend it will look like a
request for /content/site/ or /content/site/EN.html.

You may also need to rewrite the req.http.Host header to what your
backend is expecting, if it's looking for a specific hosts entry.

The only complication you'll have is that if your backend generates
URLs, it will do so using the full path (/content/site/) - depending on
what the server is what application (if any) it's running, you might be
able to override that somehow.



More information about the varnish-misc mailing list