How To Create A Basic CDN With Nginx ?

If you want to create your own basic CDN, you can easily do this with multiple Nginx servers and a load balancer. It is quite easy to setup a Nginx server, which can serve your static sources such as images, css, javascript files by caching them from their source. Once we create a simple configuration for one Nginx server, we can deploy this configuration to multiple Nginx servers and then put a load balancer in front of these in order to create a basic CDN.

How To Cache Static Assets With Nginx ?

Our first goal is to create a reverse proxy Nginx server so that this server can fetch assets from a source server and deliver these assets to users. Once we verify this server works as expected, then we will use its configuration to deploy other Nginx servers.
Assuming you have already installed Nginx on your Linux system, let’s create a virtual.conf file to reverse proxy static assets.

In Centos, we will edit the following file:

Basically, the reverse proxy configuration above tells Nginx to retrieve assets from the IP address (origin server) we have specified and caches those assets (if they do exist) for 1 month. When the static assets are fetched successfully, they will be stored under /home/nginxcache directory. This directory (you can change it from the configuration above) is managed by Nginx file system and its contents can be searched and removed as follows if we want a specific asset to be purged:

Also keep in mind that the enterprise version of Nginx, which is Nginx Plus has cache purging capability.

It is time to reload Nginx:

We can now test our first Nginx server to see if our assets are cached or not. The header response code for an asset will indicate if an asset is cached or not. For instance, when we request my.css for the first time, the header response from our Nginx server (Header will show CDN1 and cache status) will be a “MISS” because the file is not currently stored in the cache directory. However, when we request the same file again, we will see a “HIT” and the file will be stored for a month. Subsequent requests will produce “HIT” for a month period and then the cached file will be deleted if it is not requested within a month again.

How To Load Balance Nginx Servers For Our CDN ?

Since we have created our first Nginx server to reverse proxy static assets, its time to deploy more Nginx servers so that we can distribute load to them. Simply copy the virtual.conf file we have created to other servers, and then modify the “add_header X-Proxy-Serve” line in order to name each server. If we have 3 servers and named them as CDN1, CDN2 and CDN3, we can now put a load balancer in front of them to complete our CDN setup.
Even though Nginx itself can be used as load balancer, we prefer to use HaProxy for our load balancer. On a new server, simply install Haproxy and modify haproxy.conf as follows:

When we start our HaProxy with this configuration, the requests will be equally distributed to our three Nginx servers. The configuration above assumes that Haproxy can communicate with Nginx servers on a private LAN. You can change these to public IPs if you don’t have a private network. In addition, you can view statistics of Haproxy on 9000 port to verify that each Nginx server is working on our simple CDN setup.