Create A custom CDN server using — Nginx [ubuntu]

What is a CDN?

content delivery network or content distribution network (CDN) is a geographically distributed network of proxy servers and their data centers. The goal is to provide high availability and high performance by distributing the service spatially relative to end-users. source wikipedia

Will this CDN be fast enough?

If you think this CDN will be fast enough as compared with the Globally available CDN like Amazon AWS, Cloudflare then sorry this will not, as this is a single server and not distributed globally across multiple location.

However this will be definitely more faster than the normal current process as the request will be forced to a different url and the server can process them separately. Even if it is in the same server where the application is the processing will  speed up your website significantly.

Note:The process is same for creating a seperate CDN server or install in the same server. It will be a bit faster in case of a different CDN server in case of high load.

Also  you can further speed up the website by resizing the image using nginx.

Step 1  – Install Nginx: If you already run nginx on this server no need to use this step

sudo apt-get update
sudo apt-get install nginx

Step 2 – Setup your domain, using proxy pass to fetch the images on real time and store them.

sudo nano /etc/nginx/sites-available/

Step 3 – Write the following text in the file

server {
  listen       80;

  location ~* .(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)$ {
    expires max;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    proxy_store /var/www/cache$uri;
    proxy_store_access user:rw group:rw all:r;

step 4 : Ctrl+X to save the file
step 5 :give write access to the cache folder

sudo chmod 777 /var/www/cache

step 6: create symlink to live server

ln -sf /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

step 7: Restart nginx

sudo service nginx restart

Now the CDN is ready,You should now forward all static resources to this server. For the first time it will be a bit slow but after words it will be faster  as nginx will download the file from your primary server and it will cache the file to its own cache directory.


9 thoughts on “Create A custom CDN server using — Nginx [ubuntu]

  1. Good piece – we need to read more like this, because most info on this topic is done-to-death. You give real worth to your readers.

  2. I do not even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!

  3. Hi,
    Thanks for useful learning posts.
    I have a question,
    Can I have a CDN server for two website ?
    What settings should be made on the CDN server?

    1. Yes you can definitely have many CDN in one server, Just create as many virtual hosts that are required.

      1. Dear Mr. Sahu,
        Thanks for nicely described post. I want to have CDN for my upcoming video straming sites and I need your help and support.
        Please leave a message on my whatsapp 7586838127

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email