If you've used WordPress, then you know that it has a nice little folder called upload where all the pictures and videos you upload get stored. This is all fine and dandy, until people start accessing those assets.

I learned it the hard way when one of my blog posts became popular. I had a total of 17 files being downloaded after each requests. This is no big deal with the everyday traffic I was getting. (my blog was only a few months old). Suddenly, I started receiving traffic and my server went Kaput.

One thing I learned is that downloading assets shouldn't hinder your server. In fact, your server shouldn't even feel it. That's why most big websites use CDNs (content delivery networks). The advantage is that those networks serve your assets without using any processing power from your main server.

But, not every website is big and using an amazon instance is most of the time overkill for a blog or small website. That's why I decided to roll my own.

The reason for a CDN is to make sure your content is delivered as fast as possible. Some have locations all around the world and serve content from the machine closest to your user. Which is good if you are a Google or facebook, but for your blog a simple server will do the work.

I spun a new server on Digitalocean and set it up as my CDN. Most of my traffic is from the US according to Google Analytics, so I chose a server located in New York. My visitors from overseas are not having a hard time accessing me either, it is just as fast.

users location

Daily traffic location

To tell you the truth, a CDN is just a web server and there is no magic to it. All I did was install Nginx on it.

Nginx is a webserver, just like Apache. The one important difference is that Nginx is very good with serving static content. I don't need to have this machine do any processing, all I care is that it serves content. After receiving thousands of requests, my Apache web server simply died.

The motto is Nginx can serve 10,000 requests per second. I never had to serve that much content but it worked very well for me during spikes. So I set up nginx, moved all my assets to the server, and updated the DNS record. Now all my static files are served from cdn.idiallo.com. Very easy.

This works for me because I use a very primitive process for uploading my files, at least I used to (scp). If you are using a WordPress installation it will be complicated to customize the code to upload the files to a different server instead. But there is a simple solution.

Shared folders. If you are hosting with shared hosting providers you are out of luck. But for others, you can make your upload folder and shared folder, accessible from your CDN machine. This way you upload in one machine and it automatically reflects on the other.

Once your CDN is setup properly, you never have to touch it. All it does is serve files and update when you make new files available.

Installing Nginx

If you are using a debian machine all you need is this command:

sudo apt-get install nginx

Choose a folder where you want your asset folder to be: (/var/www/assets/). Then you can point Nginx to it. Create an Nginx config file:

vim /etc/nginx/sites-available/yourwebsite.com

And add these settings:

server {
  listen 80;
  server_name cdn.yourwebsite.com;

  location / {
    expires 90d;
    root /var/www/assets/;
  }
}

Also make sure you create a symlink in the sites-enabled folder.

That's all there is to it. You can restart your server and start serving files from your own custom CDN now. You can update your DNS to use your new CDN. subdomain, which should be available in your hosting provider GUI.

Enjoy serving fast content without breaking your server.

Update:

If you want to upload files on your webserver but want to make them available on your CDN server, here is how you can share files between your webserver and CDN