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

  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?


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


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


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..

  • 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 1 '11 at 4: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 1 '11 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.