How to build your own CDN

Shubham Kumar
5 min readNov 18, 2021
CDN Network

In 2020, the entire world created content amounting to 2.5 quintillion bytes per day. In 2021, Globally the Internet traffic has reached 30 GB per Capita as compared to 10GB per capita in 2016.

This rise has made the need for CDN even more important. Handling a large amount of data exchange requires a proper backbone. Unlike Commercial CDN, a private CDN gives us full control of the content as we don’t share the servers/content/machine with other websites using the same infrastructure.

Today we are building our private CDN. We will be using technologies and sites as follows:

  1. FreeNom: It is a domain provider which is totally free of cost.
  2. Digital Ocean: We will be using the digital ocean for server hosting.
  3. Nginx: The Super Cool Web server which serves all purposes.

So let’s get started. I am making sure to make this whole DIY CDN process to be free.

Starting with Freenom let’s get head over to freenom.com and signup for a free domain. After signing up head over to Register Domain Page. You can search your preferred domain.

After getting a domain. let’s get over to creating the droplet(refers to server) in the digital ocean. We are creating two droplets over here one in the US region and the other in the Bangalore region. By doing this, we will be delivering the content to our users from the nearest server to their location based on the region.

You can create a free account on the digital ocean which provides you 100$ credits for 60 Days. So let’s create a droplet for the India location.

Once we have created the droplets for both US and India regions. So we have now two hosted servers. We will now jump to our second part of configuring the installing the Nginx.

Nginx is a web server that can be used can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. Here we are using it as a Geo Redirection tool and also an HTTP cache. We will be serving content to our users from the nearest available server, thus decreasing the load time and improving the user experience.

Nginx serves the purpose in two steps:

  1. Working as a traffic redirector, it redirects the request to a continent-based server based on the IP.
  2. It caches the data from the primary server to the nearest server which in our case is in the US.

So for the first time, user-based in India comes to download the content from the site, the request is redirected to the India server and it caches the data. For subsequent requests, data is directly served from India based server. This was the load time is reduced to almost half. Now, let’s get back to coding.

Let’s install the required repositories:

sudo apt-get update
sudo apt-get install nginx-full geoip-database

We also need geo city-data binaries to find the city based on IP.

wget -N https://raw.githubusercontent.com/mbcc2006/GeoLiteCity-data/master/GeoLiteCity.dat
mv GeoLiteCity.dat /usr/share/GeoIP/

Building the Geotool

The first thing to do is add GeoIP-based information to redirect the request.

Head over to (nginx.conf) /etc/nginx/nginx.conf with nano/vim/etc.Add the following segment of code. Nginx will be updated like this.

http {
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
}

Save it and let’s update the virtual host Let’s connect the freedom domain with servers. We will be replacing the default nameservers in freenom domain with custom digital ocean servers. This option will be under the Manage Domain> Management Tools>Nameservers.

Since we have configured the domain and also installed the servers. Let’s allow the Nginx rules on both servers to accept the request. We are now enabling HTTP Requests on Nginx.

$ sudo ufw allow 'Nginx HTTP'

You should be able to view this content. Let’s see if we are at the same step.

Try opening argocdn.tk and you will be redirected to in.argocdn.tk ( this is server-based in India). Try opening in.argocdn.tk on VPN pointed to the US region and you will be redirected to argocdn.tk. This completes our first objective on the Geo Redirection tool.

Implementing the Caching

Now we have implemented the geo redirection, we will be moving to implement the caching part.

We will cache the files being served from central (us) to the nearest available server. As in our case, for users in Asia, data will be cached on in.argocdn.tk. For the first time, it will download the data from our us server and makes caches, for subsequent data is served from the cache.

The modified virtual host(/etc/nginx/sites-available/default) for servers other than will contain the caching code.

//Defining the proxy catch path along with parameters:proxy_cache_path /var/www/cache levels=1:2 keys_zone=cdn:10m inactive=60m;server {location / {
proxy_cache cdn;
// Defining the cache key
proxy_cache_key $uri$is_args$args;
proxy_cache_valid 90d;
proxy_pass http://argocdn.tk;
}
}

Let’s understand :

  • /var/www/cache is the place where the actual cache is stored. Inside the folder, the cache file was a binary file but you can easily spot the HTML tag inside it.
  • levels=1:2is the levels parameter that sets the number of subdirectory levels in the cache.
  • keys_zone=CDN:10m was defining a shared memory zone named myCache with a maximum size of 10 MB. It holds all active keys and metadata of the cache. So, whenever Nginx checks if a page was cached, it consults the shared memory zone first, then seeks the location of the actual cache /var/www/cache if the cache exists.
  • inactive=60m specify the maximum inactive time cache that can be stored. Cached data that are not accessed during the time specified by the inactive parameter get removed from the cache regardless of their freshness.

Here is the CDN we have built. Let’s test it.

Trying opening argocdn.tk/argo.jpg, you will be redirected to in.argocdn.tk/argo.jpg. We are redirected to the India server. Also, we can see that if we reload the URL the amount taken will be halved.

Before Caching it will take around 1.29 seconds.

After caching it will take 77ms.

Hope you liked the article : ). Clap, share, and follow to get the latest article updates.

You can also connect with me on Linkedin or Instagram.

--

--

Shubham Kumar

SDE-2 Expedia, ex-Airtel, Nagarro | OSSF London Scholar 2021 | HGF Scholar 2020 | Facebook F8 Scholar 2019 | Fossasia Finalist Winner 2019