0

Optimizing page load speed practices include to load static assets, like images, css, js, from subdomains, so to increase performance, by using cookieless domains, but most importantly by having parallel downloads from the browser.

I use varnish reverse cache in front of my webserver which serves various websites.

  1. How I can instruct varnish to rewrite url for static files (css,js,png,jpg,jpeg,gif), so they are served by a subdomain? So if someone asks for domain.com/image.png this to be finally served from static.domain.com/image.png. Domain and subdomain are served from the same server. Varnish already connect to the backend at 127.0.0.1

  2. Is it more efficient to do the url rewriting at varnish or at the backend (apache)? I think it would be faster to be done in varnish, since it's at the frontline, right?

  3. What is the way to do the same in apache?

john
  • 137
  • 5

1 Answers1

1

varnish really isn't made to do redirects. There are some hacks out there to do it like for example

http://blog.mudy.info/2009/02/how-to-do-http-redirect-on-varnish/

I personally would not do it that way. The correct way to go about this is in the web app. You change the code to reference different hosts to get the assets from.

So you have

cdn1.domain.com
cdn2.domain.com
...
cdn10.domain.com

That way you can get around tricking the browser better. And they all hit the same IP and your httpd config just has those hosts in a alias for the main domain.

You will actually slow things down doing the redirect due to the client making another request once it gets the redirect. What you want to do is really a bandaide fix to the solution that will show little speed improvements..

Mike
  • 21,910
  • 7
  • 55
  • 79
  • Thanks for your answer. How I can express in code the following logic: sub vcl_recv { if (req.url ~ "\.(png|gif|jpg)$") AND req.http.host=example.com THEN REDIRECT to cdn1.example.com/requestedpic.png ? – john Jun 01 '11 at 04:13
  • like I said.. its not a good idea to do that.. Your browser will make 5 parallel connections.. wait for the redirects.. then another 5.. You pretty much are at the same speeds as not doing it at all.. – Mike Jun 01 '11 at 12:15