Configuring htaccess file for self hosted CDN


I am trying to set up a self hosted cdn for my site, but I am having problems with the rules in the htaccess file and I can't get it to work.

At the moment the ftp server that will act as CDN has the following filesystem structure:


My .htaccess looks like this right now:

Options All -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

#This snippet basically stops anyone viewing any file on your site that begins with "hta", this will protect it and make it somewhat safer.
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all

<Files /cdn>
   Options +Indexes
   Satisfy Any
   Allow from all

I want to allow access to the cdn folder and disallow any other access to the other folders, because in incremental and full there are backups of my main server.

I've been searching for rules for achieve this behaviour but nothing work.

I have to allow Indexes in the cdn folder?

Regards and thank you in advance (And sorry for my english)

Best Answer

Not to be a jerk but this doesn't sound at all like you're building a CDN. The whole concept of a CDN hinges on delivering your users content from a node closest to them vs. your users having to trudge through the internet to a single box.

It's possible in your research you're having trouble finding out what to do because you're adding this "CDN" concept when it's really not pertinent.

I'll echo the previous advice of moving your backups out of the web accessible folder!

Given what I believe you're trying to accomplish I'd advise that you setup separate vhosts that point requests for a given FQDN to a folder. This will give you more control over your configuration and ensure that data is only available to certain domains. So you'd add a new file (myconfig.conf, anything really) in your apache2/conf.d directory that looked something like this:

<VirtualHost *:80>
DocumentRoot /mnt/www/DOMAIN_NAME/cdn
ServerName cdn.DOMAIN_NAME

Obviously you can use whatever you want for your document route, the above is just my personal style. Create the directory path /mnt/www/DOMAIN_NAME/cdn replacing the DOMAIN_NAME with whatever you're using. Then anytime you hit cdn.DOMAIN_NAME it will ONLY serve up files in the /mnt/www/DOMAIN_NAME/cdn directory. Obviously this requires some adjustments to DNS to ensure the cdn subdomain of the DOMAIN_NAME used is pointed to this box.

EDIT: Don't forget to restart apache after you add the new .conf file!

Hopefully this helps. Though if a true CDN is what you need, I'd advise you to use a service like s3/cloudfront or Rackspace Cloudfiles rather than a DIY solution. They're VERY affordable options.

Related Topic