OpenSource For You

Use Content Delivery Networks to Speed Up Your Website

A content delivery network (CDN) has multiple servers, which are geographic­ally distribute­d for the easy delivery of Web content. A CDN enhances the performanc­e and speed of delivery of such content.

- By: Jayashree N. The author is a cloud specialist working with Fcoos Technologi­es. She has expertise in providing technical solutions related to cloud services, particular­ly on AWS, Azure and OVH, apart from multiple other platforms.

One of the significan­t ways of enhancing website performanc­e is to implement a CDN to serve compiled assets in Rails.

The problem

Though the addition of an asset pipeline decreases the number of assets served and the file size, yet the speed at which the contents are transmitte­d to the end user is slow. Distance also plays a role in the speed at which data can be delivered. Because of slow connection­s, the user’s patience decreases, and so does your ability to effectivel­y engage them.

The solution: Content delivery networks (CDNs)

CDNs are networks of servers that host your content so that when you make a request, the request is served from the server closest to you. The use of a CDN also reduces the number of requests hitting your applicatio­n server (Apache/ nginx). Nginx default site configurat­ion for Ruby on Rails is given below:

upstream puma {

server unix:///home/ubuntu/apps/ sockets/;


server { listen 80; server_name; root /home/ubuntu/apps/; access_log /home/ubuntu/apps/ access.log;

error_log /home/ubuntu/apps/ error.log info;

location ~ ^assets/ { root /home/ubuntu/apps/;

gzip_static on; expires max;

add_header Cache-Control "public"; access_log /dev/null;

} location ~* .(js|css)$ { gzip_static on; expires 30d;

add_header Cache-Control "public"; } location ~* .(ico|gif|jpg|png|svg|JPG|jpeg|webp)$ { gzip_static on; expires 1M; add_header Cache-Control "public"; }

try_files $uri/index.html $uri @puma; location @puma {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_ for; proxy_set_header Host $http_host; proxy_redirect off;

proxy_pass http://puma; } error_page 500 502 503 504 /500.html; client_max_body_size 10M; keepalive_timeout 10;


CDN configurat­ion

In this configurat­ion, the CDN pulls assets from your Web server and caches them. All subsequent requests to that asset will be served straight from the CDN.

1. Login to AWS console and select Cloudfront as shown in Figure 3.

2. Click Create Distributi­on as shown in Figure 4.

3. Enter the domain name where your assets are currently located as shown in Figure 5.

4. Customise the object caching. Minimum TTL is set to one day.

5. Enter the alternate domain name, if any, and keep the rest as defaults.

6. Make note of the Cloudfront distributi­on URL.

7. Enter the Cloudfront distributi­on URL in the Rails asset_ host to change the host URL of the assets, as follows:

# config/environmen­ts/production.rb config.action_controller.asset_host = ""

With this, site performanc­e will be increased and the number of requests hitting the applicatio­n server will also be reduced.

 ??  ??
 ??  ?? Figure 1: Nginx configurat­ion
Figure 1: Nginx configurat­ion
 ??  ?? Figure 5: Enter the domain name
Figure 5: Enter the domain name
 ??  ?? Figure 4: Create a distributi­on
Figure 4: Create a distributi­on
 ??  ?? Figure 2: Nginx configurat­ion (contd)
Figure 2: Nginx configurat­ion (contd)
 ??  ?? Figure 3: Select cloudfront
Figure 3: Select cloudfront

Newspapers in English

Newspapers from India