ok if you're looking at drupal 6 look into custom_url_rewrite_outbound(). For D7, you can use hook_url_outbound_alter().
Some example code I have sitting around in a module that takes media resources of a Drupal site and points the generated urls to another server to serve media content, we used Nginx. Keep in mind that if you're just sending back to same server on another port .... you're just feeding yourself more traffic -- which isn't as helpful as a true CDN.
/*
*
* A mechanism in Drupal to rewrite outbound urls to point to another domain.
* This can be used to serve static content from a dedicated server, aka a CDN.
*
* Taken 100% from the following url. I am not smart enough to pull off this
* magic :).
*
* http://drupal4hu.com/node/260
*
* In the below code $conf['dew_cdn_domain'] is a custom value in
* settings.php that points to like "http://yourotherserver.com" to serve
* media files.
*/
/**
* Implementation of hook_file_url_alter()
*
* This inspects and transforms file urls to our remote CDN host.
*/
function dew_cdn_file_url_alter(&$uri) {
global $conf;
static $drupal_static_fast;
// A drupal_static_reset() would clear this and then we need to rerun variable_get.
if (!isset($drupal_static_fast['dew_cdn_domain'])) {
$drupal_static_fast['dew_cdn_domain'] = &drupal_static(__FUNCTION__);
$drupal_static_fast['dew_cdn_domain'] = $conf['dew_cdn_domain'];
}
$cdn_domain = &$drupal_static_fast['dew_cdn_domain'];
if ($cdn_domain && strpos($uri, ':') === FALSE) {
$uri = $cdn_domain . $uri;
}
}
/**
* Implementation of hook_stream_wrappers_alter()
*/
function dew_cdn_stream_wrappers_alter(&$wrappers) {
$wrappers['public']['class'] = 'DrupalCDNPublicStreamWrapper';
}
/**
* Any files that would go the public stream public:// get rewritten to another
* base domain.
*/
class DrupalCDNPublicStreamWrapper extends DrupalPublicStreamWrapper {
function getExternalUrl() {
global $conf;
$path = str_replace('\\', '/', $this->getTarget());
return $conf['dew_cdn_domain'] . self::getDirectoryPath() . '/' . drupal_encode_path($path);
}
}