Host your static site on Cloud Files, the CDN network from RackSpace

Written by
Date: 2012-07-10 20:45:00 00:00


The vast majority of sites on the web now a days are dynamically generated, even when most of them can be static. This is done because that way is easy to create a site. Using a CMS.

But, if your site is 100% static, and you do not need to create your site on the fly, dynamically, you can get the advantage of hosting it in a CDN network. Yes the complete site hosted in a CDN network, not only the images or css files, the whole site.

There are plenty of instructions to do that in Amazon S3, and then enable Amazon Cloudfront, to use the advantages of the CDN. I'll show you here how to do the same using Cloud Files from Rackspace, and use Akamai CDN network to distribute your site all over the world.

Note: Rackspace uses the Akamai Network, but it seems a limited set of endpoints of it

Note 2: I'll assume you already have a static site, with an index.html file that is the head of the site. I'll also assume that you have a Rackspace account

Upload your site to Cloud Files

Rackspace Cloud Files does not let you create folders or sub-folders, you need to create Pseudo-Hierarchical Folders/Directories. That is not easy, but you can use Cyberduck. Cyberduck will take care of "sub-folders" for you. All you have to do, is drag-n-drop the files.

You now have all your files uploaded, and with the correct structure.

Enable the CDN for the container, where you have uploaded your static site

Log in to Rackspace manager, go to Hosting->Cloud Files, select the container you want to work with, and tick the Publish to CDN option.

Tell Rackspace which is your index file

Now you have your site published to the CDN, you can access any file if you have the complete url. For instance:

http://www.example-site.com/how-to-use-rackspace.html

or

http://www.example-site.com/images/picture1.jpg

But what happens when you try to access: http://www.example-site.com/, you will get a 404 "file not found" message, you need to specify which is the index file for your site's main folder and sub-folders.

Something like Index in Nginx or, DirectoryIndex in Apache.

To do that, you need to send a POST message to your Storage-Url, with the header X-Container-Meta-Web-Index: index.html if your index page is index.html or any other you would prefer.

For you to be able to send this message, and for it to be accepted, you first need to authenticate yourself. For the sake of clarity, let start with the complete example of the POST message you need to send, the complete message is (using curl):

curl -X POST -H "X-Container-Meta-Web-Index: index.html" -H "X-Auth-Token: aaaaaaaa-0000-0000-0000-aaaaaaaa" https://storage100.dcw1.clouddrive.com/v1/MossoCloudFS_1234567a-123c-12d1-7cad-a09878abcde3/your_container/ -v

As you can see, you need something to put in X-Auth-Token field, to get that value you have issue this other command in your Mac or Linux terminal:

curl -H "X-Auth-User: your-user-name" -H "X-Auth-Key: 0123456789abcdef01234567899d" https://auth.api.rackspacecloud.com/v1.0/ -v

Where:

  • X-Auth-User: Is the username you use to log into your Rackspace account
  • X-Auth-Key: Is your API key.

To get your API key, go to: Your account -> API Access and click Show Key button.

Once you enter that command in your terminal, you will get an answer like this:

< HTTP/1.1 204 No Content
< Server: Apache/2.2.3 (Red Hat)
< vary: X-Auth-Token,X-Auth-Key,X-Storage-User,X-Storage-Pass
< X-Storage-Url: https://storage200.dfw1.clouddrive.com/v1/MossoCloudFS_0002-abcd-0993344-adfe
< Cache-Control: s-maxage=59568
< Content-Type: text/xml
< Date: Tue, 10 Jul 2012 10:29:21 GMT
< X-Auth-Token: aaaaaaaa-0000-0000-0000-aaaaaaaa
< X-Server-Management-Url: https://servers.api.rackspacecloud.com/v1.0/000111
< X-Storage-Token: aaaaaaaa-0000-0000-0000-bbbbbb
< Connection: Keep-Alive
< X-CDN-Management-Url: https://cdn1.clouddrive.com/v1/MossoCloudFS_0000000-1111-2222-3333-9999999
< Content-Length: 0

From there you can get the X-Auth-Token value you need to use with this command:

curl -X POST -H "X-Container-Meta-Web-Index: index.html" -H "X-Auth-Token: aaaaaaaa-0000-0000-0000-aaaaaaaa" https://storage100.dcw1.clouddrive.com/v1/MossoCloudFS_1234567a-123c-12d1-7cad-a09878abcde3/your_container/ -v

With this, you have have finished the configuration, you can now create a CNAME in your DNS to point your www.example-site.com to point to your CDN url. Do not forget to set a proper TTL for your files, you can do that in the Cloud Files section in the Rackspace control panel. The default is 72 hours.