Create a Personal 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. You can then use this server to serve static assets on your website for example. 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 back up all configuration files.

mkdir backups
mv *.conf backups

Step3. Create a new configuration file called pass.conf using your own favorite editor:

vim pass.conf

Step4. Put the 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.

© 2021 metinsaylan