How to create your own CDN server using Nginx

Nginx is an event driven web server, that is why it is a perfect choice to create your own CDN server. In order to build your own cdn server, simply follow these steps:

Step1. Install nginx on your hosting server.

yum install nginx

Step2. goto /etc/nginx/conf.d folder and backup all configuration files.

mkdir backups
mv *.conf backups

Step3. Create a new configuration file using:

vim pass.conf

Step4. Put following text into that 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;

Step5. Save nginx configuration using :wq and quit.

Step6. Add nginx to startup services.

chkconfig nginx on

Step7. Start nginx.

service nginx start

For this configuration to work, you should create a cache folder under /var/www and it should have appropriate folder permissions for nginx to work.

You should now forward all static resources to this server. If the file is not found, nginx will download the file from your primary server and it will cache the file to its own cache directory.

Have fun.