Content Delivery Networks (CDNs) can be used in concert with OpenShift to rev up the performance and availability of your websites.
OpenShift’s auto-scaling feature equips web applications to handle increases in requests for site content, which may or may not be dynamically generated. CDNs, also known as Content Distribution Networks, can help to reduce some of the load on your OpenShift application servers and improve your site’s performance by caching static resources such as images and scripts and serving them up from the closest location to your users, from a global network of servers distributed across the internet.
CDNs can also help to protect your web applications by detecting suspicious behavior and blocking offending traffic.
There are many CDNs to choose from, such as Akamai, CloudFront, Incapsula, and CloudFlare. In this blog post, I will show how to add the CloudFlare CDN to an OpenShift-hosted website with a custom domain. CloudFlare has a free tier, so anyone can use it.
Step 1: Create CloudFlare Account
The first step is to sign up for an account at CloudFlare.com. To do so means filling out a simple web form.
At this point, it’s worth giving some thought to whether or not you need SSL support from your CDN. CloudFlare has a Free plan that can be used with your OpenShift site, but it does not currently offer SSL support, although that may not always be the case.
You could choose to use the CDN for some parts of your site only and enable SSL on the remaining parts. If you do not want to follow this approach or need CDN-powered SSL, you should select a paid plan.
For this blog post, I will assume you wish to use CloudFlare with an OpenShift application using SSL with a custom certificate; to add a custom SSL cert to your application, you must be on OpenShift Online’s Bronze or Silver plans.
Step 2: Add OpenShift Website
Next, continue to the Add a website form or sign in to CloudFlare and go to the Websites page. Type in your OpenShift site’s custom domain name, which I will call www.yourdomain.com, and click Add website.
The domain you enter must be a custom domain, set up with an alias on OpenShift through RHC or the web console. CloudFlare will not work directly on appname-domain.rhcloud.com URLs because OpenShift Online users cannot control the nameservers for rhcloud.com.
One you have added the website, CloudFlare will retrieve the site’s current DNS records.
Step 3: Configure DNS Records on CloudFlare
CloudFlare will present you with a list of site DNS records. The screenshot below shows this for my website lambdaladies.com, which is hosted on OpenShift Online.
You can choose which parts of your site are directed through the CDN and add or delete records if you wish. If your site DNS is set up properly, you may not need to touch the records at all.
The service automatically creates a new ‘direct’ subdomain for direct application access, that can be used to bypass the CloudFlare network when you are using the domain to access services such as SSH or FTP. This record can be deleted if you wish as it is not necessary for OpenShift sites, given you can bypass the CDN via the appname-domain.rhcloud.com URL, which is used by tools such as RHC for SSH access.
CloudFlare allows users to create CNAME entries for naked domains, such as yourdomain.com, but this is not considered good practice and can cause mail sent to your domain to bounce. If you did want to do this, you would delete the A record and add a CNAME for yourdomain.com pointing to appname-domain.rhcloud.com. There is more discussion about alternatives in this blog post.
Once you are done configuring your DNS records, click I’ve added all missing records, continue.
Step 4: Configure CDN Settings
On the next page, you select your CloudFlare plan, performance level, security level, whether or not SSL should be enabled, and a few other options.
As already mentioned, SSL support is not currently available on CloudFlare’s Free plan. Therefore, for our example URL yourdomain.com, we would pick the Pro plan and turn on SSL support, as we are using SSL for our OpenShift app with a custom SSL certificate. The other options depend on your preferences.
When you have selected your settings, click Continue.
Step 5: Update Name Servers
At this stage, CloudFlare will give you the URLs of some nameservers. You need to log in to your DNS provider and configure your domain to use these nameservers.
The process for this will be different for each DNS provider. For lambdaladies.com, which is registered with Namecheap, the process was to:
- Log in to the Namecheap account
- Go to Manage Domains and click on lambdaladies.com, which took me to a Modify domain page
- Select General > Transfer DNS to Webhost
- Select Specify Custom DNS Servers and enter the CloudFlare nameserver URLs
- Click Save Changes
The CloudFlare Support section has instructions for other DNS registrars.
Once you have added the nameservers, click on I’ve updated my nameservers, continue and you should see a ‘Congratulations!’ page.
Step 6: Enjoy
If you have followed along, your OpenShift site is now configured to use the CloudFlare CDN. Once the change takes effect (DNS changes can take some time to propagate), you can test out the performance difference with your browser’s developer tools or a website such as WebPageTest.org.
There are also other changes you may like to make on CloudFlare to enhance your site. There are many settings you can tweak by going to the Websites page, selecting the gear icon to the right of your site, and selecting CloudFlare settings. You can click on Apps, Analytics, and Threat Control to see other capabilities you can add and manage.