Explain in Detail how to Build your own CDN?

  • 2022-12-05
  • Author:Korims

In the process of building our own CDN, we should have a detailed understanding of CDN, so what is CDN? Specifically, CDN uses more cache servers (CDN edge nodes) and deploys them in areas or networks where user access is relatively concentrated. When a user visits a website, the global load technology is used to direct the user's visit to the nearest cache server, and the cache server responds to the user request. In layman's terms, the purpose of deploying CDN is to increase access speed and improve the speed of users' access to the Internet or a certain site or APP.


自建CDN


Why do you need to build your own CDN? There are three reasons:


1. To use a domestic CDN server domain name, it needs to be filed.


2. The stability and acceleration effect of the free CDN server is not very good.


3. Paid CDN servers are generally very expensive and cannot be used at all.


Assume that the Baidu website has only one server now, and a person visits Baidu in Shenzhen. If the server is also in Shenzhen, the access is generally faster. If the server is in Xinjiang, the access is relatively slow. Then the root cause of this problem is that network transmission depends on the network cable. The longer the network cable, the longer the time must be.


So how can you build your own CDN? The method of reverse proxy self-built CDN through Ngnix


Reverse proxy: The reverse proxy (Reverse Proxy) method refers to using a proxy server to accept connection requests on the Internet, then forward the request to a server on the internal network, and return the result obtained from the server to the Internet requesting connection client, the proxy server acts as a reverse proxy server externally.


Specific operation process:


First install Nginx


wget https://raw.githubusercontent.com/helloxz/nginx-cdn/master/nginx.sh

chmod +x nginx.sh && ./nginx.sh


Complete operation example: Website name: www.xxx.com Replace your own website address by yourself


Source site: aaa.aaa.aaa.aaa server address of original data and services

CDN1:bbb.bbb.bbb.bbb (telecom node)

CDN2:ccc.ccc.ccc.ccc (China Unicom node)

CDN3: ddd.ddd.ddd.ddd (mobile node)


We need to modify the host pointing of the CDN1/2/3 server:


vi /etc/hosts

aaa.aaa.aaa.aaa www.xxx.com


Create the nginx configuration file xxx.com.conf under CDN1/CDN2/CDN3 respectively


mkdir -p /data/wwwroot/caches/www.xxx.com#Create a cache directory

chown -R www:www /data/wwwroot/caches/www.xxx.com#Set cache directory permissions

vi /usr/local/nginx/conf/vhost/xxx.comconf#create xxx.com.conf


Add in xxx.com.conf:


/data/wwwroot/caches/www.joymagic.com: cache directory

levels: specifies that the cache space has two layers of hash directories, the first layer directory is 1 letter, and the second layer is 2 letters.

keys_zone=caches:100m: Give the cache space a name, here it is named "caches", and the following 50m refers to the memory cache space

inactive=30m: delete if the resource is not accessed within 30 minutes

max_size=100m: refers to the hard disk cache size is 100MB

proxy_cache_valid: Specify the status code cache time, write the status code in front, and write the cache time in the back.


Reload nginx to make the configuration take effect: service nginx reload


Intelligent analysis


In the CloudXNS background, different operators are pointed to different nodes to achieve distribution and cache acceleration. Point the domain name's www A record to:


CDN1:bbb.bbb.bbb.bbb (telecom node)

CDN2:ccc.ccc.ccc.ccc (China Unicom node)

CDN3: ddd.ddd.ddd.ddd (mobile node)


Finally, use http://ping.chinaz.com to test whether the local analysis is effective.