This article will show you some mod_rewrite wizardry to get your static content served from your own “cdn” domain until CNAME support is here.

Requirements
This article assumes you know how to get the public URL for your container, and are familiar with uploading files to the Rackspace Cloud platform.

All implementations require an Apache web hosting account with mod_rewrite enabled.

You may use either a subdomain (eg: cdn.domain.com) or a subdirectory (eg: www.domain.com/cdn) to implement this. I recommend using a subdomain.

Implementation #1

This implementation is the simplest approach and simply redirects all users to the CDN. You are limited to one bucket with this approach.

In the base directory of your subdomain you will need to create a ‘.htaccess’ file that looks like the following:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://c0918182.cdn.cloudfiles.rackspacecloud.com/$1 [R=302,L]

You will need to replace ‘c0918182’ with the ID of your container.

The rewrite condition will not redirect requests for / to Cloud Files. This allows you to create an index file. We’re using a 302 temporary redirect (instead of 301 permanent) so search engines will pick up the URLs under your custom subdomain (and not the Cloud Files URL).

Implementation #2

This implementation is similar to implementation #1, except it allows you to have multiple containers so you can segregate your content. For example, this will allow you to have http://cdn.domain.com/images/ and http://cdn.domain.com/files/ going to two different Rackspace Cloud containers.

We’re going to use ‘images’ as our first example.

To get started, create the directory in your subdomain.

In this directory, create a ‘.htaccess’ file that looks like the following:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images/$
RewriteRule ^(.*)$ http://c0918182.cdn.cloudfiles.rackspacecloud.com/$1 [R=302,L]

You will need to replace “/images/“ with the name of your directory, and replace ‘c0918182’ with the ID of your container.

Like implementation #1, the rewrite condition will allow you to create an index file in your directory.

To link additional directories to different containers, simply repeat the instructions with a different directory name and container ID.

Nginx Implementation

If you’re running Nginx, you can accomplish the same effect using locations and rewrites. This will be similar to implementation 2 for Apache. Any file that does not exist in the file (such as an index file, or some arbitrary file you felt like putting there) will be served off of the CDN.

In your Nginx config, you’ll create a new server directive for your subdomain as well the location block for your redirects.


server {
    listen  80;
    server_name cdn.failverse.com;
    access_log /var/log/nginx/cdn.failverse.access.log;

    location / {
       root /var/www/vhosts/domain2/public/cdn;
       index index.html;

        if  (!-e $request_filename)
           {
                rewrite ^/images/(.*)$  http://c0918182.cdn.cloudfiles.rackspacecloud.com/$1 redirect;
                rewrite ^/files/(.*)$ http://c0918176.cdn.cloudfiles.rackspacecloud.com/$1 redirect;
                        
            }
          }
       }

You will need to replace /files/ or /images/ with the relevant directory and ‘c0918176’ with the container name. You are able to create as many redirects this way as you want.

Lighttpd

If you use Lighthttpd, you can use the following configuration to accomplish the same thing.

$HTTP["host"] == "cdn.failverse.com" {
url.redirect = ( "^/images/(.*)" => "http://c0918182.cdn2.cloudfiles.rackspacecloud.com/$1",
"^/text/(.*)" => "http://c0918182.cdn2.cloudfiles.rackspacecloud.com/$1")
}

tadalafil shop however the domain is down, and the article is a bit outdated.

« »