views:

173

answers:

4

It's generally considered a best practice to serve static content, such as images and css, from different sub domains (images1.domain.com, images2.domain.com, etc). I've seen this discussed in detail in various places, however I'm concerned about the general logistics of this in terms of maintainability.

Our site has thousands of pages and changing all references to static content from relative to absolute links pointing to different sub domains sounds like a nightmare, what are some general approaches to accomplishing this? Is there any way to automate this, perhaps with a server that sits in front of the web server and rewrites incoming requests to point to different sub domains? Are there any frameworks built with this in mind? Is the performance gain worth the added complexity and maintenance costs?

EDIT: Our environment is ASP.NET 3.5 SP1 on IIS 7.

+1  A: 

I did this once on the small scale using mod_rewrite (e.g. one Google link: http://corz.org/serv/tricks/htaccess2.php)

The reason I chose this is because I had certain file extensions that were all on the separate server. Anything with that extension moved straight over to the same file structure on the subdomain. Might not be the same situation you have.

This is a small scale solution, not sure of the performance implications of doing this for thousands. For that, I might try a script for textual search/replace.

JohnMetta
I could see how this would work for one sub domain, but ideally we'd want 2-4 sub domains. We could just randomly pick a sub domain, but this would break the browser cache.
Chris Jackson
Ack. Yeah, I'm thinking really small scale here- outta my league. Every once in a while you run across that problem that causes you to say "CARP! There's no way to do this but the hard way!" I really hope someone smarter than me keeps you from having to say that.
JohnMetta
A: 

One possible approach might be to use a client-side Javascript (jQuery?) to go over all the relevant links and change them to point to the static location.

Since you want to use several domains without breaking browser cache I'd use some hashing on the filename to make sure that name always points to the same subdomain...

Eran Kampf
Not very likely to work: you have to make sure you can change the URL before the browser sees it and starts dowmnloading the file.
Julien
Well the only other way I could think of is to do the same on the server. Using some module that can process the HTML before sent to the client.Pretty much like the guys at RPO are doing optimizations (http://www.aptimize.com/)
Eran Kampf
So far this seems to be the only elegant way to do this. Know of any modules already built to do this with ASP.NET / IIS?
Chris Jackson
A: 

Ewww!! Sounds like highly-premature optimization to me. Unless you benchmark this carefully, I'd put this 'improvement' last on my to-do list.

If you really want to do that, you should add an extra layer of indirection while generating your HTML (pseudo-JSP syntax follows). Instead of:

<img src="http://images.mysite.com/image/image1.gif"/&gt;

do

<img src="${image:url(image1.gif)}"/>

and have image:url generate a random virtual host or something; you can at a later date redefine how image:url works.

If this is not possible, I believe Eran Kampf's answer is the right one; in Java-parlance you would use a ServletFilter.

alex
The problem with this solution is that you'd have to do it for every single image file on the site and it also wouldn't work for css and js. The better solution would be to have something process the entire request, which I believe is what Eran Kampf is referring to.
Chris Jackson
A: 

I did this once but luckily the people before me hadn't been using relativel links, they had used a constants file (for the most part) so it was relatively easy.

I think it's a good idea to do, it will open up your options for scalability. Perhaps search you code base for .jpg and .gif and create a macro to append the subdomain to the root of the path. Be sure to create a single function that returns the subdomain so you can house the logic for which subdomain to choose in a central location.

Cameron