The distance between the location where a web service is hosted and the locations of users that access its content can have a great impact on the effective page load time. For instance, australian and american users of a web site located in Spain or Germany will experience a noticeably longer page load time than european users.
This problem can be avoided with the help of a Content Delivery Network (CDN).
This post explains the characteristics of this kind of service, with the example of a wordpress site that is configured to make use of the CloudFlare CDN.
What is a CDN and how does it work
A CDN is a distributed network of servers with nodes in several locations of the world, acting as an intermediary between the browser that requests a web page and the server that delivers the content.
The user that wants to access a given page enters the corresponding URL in the browser, or clicks on a link that points to that URL. URLs start with the prefix “http://” or “https://” followed by the name of the server. For instance, http://server-name/page-name. The first step done by the browser is contacting a name server (DNS server) to retrieve the IP address corresponding to the server name in the URL. Then, the request is sent to the IP address obtained.
If a CDN service is not being used, the IP address returned by the DNS server is that of the destination web server, and the request is sent directly to that server:
But, if a CDN service is being used, the IP address returned by the DNS server is the IP address of the CDN node closest to the browser. The browser will then send the page request to the CDN node, not to the destination server.
The first time that a CDN node receives a request for a given page, it will forward the request to the destination web server. The response received will be delivered to the browser that issued the request, and a copy will be locally cached in the CDN node itself:
The second and following times that the CDN node receives a request for the same page, it will deliver the locally cached copy to the requesting browsers, with no intervention of the web server:
The CDN nodes will continue serving locally cached content without the intervention of the destination server, until the configured expiration time is reached.
In this way, two goals are met:
- Client browsers get the page content served by a close CDN server, reducing the network latency.
- The load posed to the web server is reduced noticeably, because most requests are served directly by the CDN nodes.
CloudFlare is a CDN service with emphasis on the security and the protection against spam and DDos attacks. Besides, CloudFlare offers a free service for blogs and other small websites. It also offers paid plans starting from $20/month, with greater performance and configuration flexibility.
As such, CloudFlare is generally a good choice for the implementation of a CDN service for a small wordpress blog with an international audience:
Creting a CloudFlare account and setting up DNS
The first step to make use of the CloudFlare CDN service is creating an account. The sign up process is fairly simple, just go to the CloudFlare site, click on the “Sign up” link, enter the data requested in the sign up form, and send it clicking on the “Create account now” button:
When this initial analysis completes, a form with the identified records is displayed. We are then given the option to continue adding records for subdomains that must be included in the DNS configuration. There is a cloud symbol to the right of each record. If the symbol is orange, the corresponding subdomain will use the CDN service. If the symbol is grey, the CDN service will be bypassed, and all requests received for that subdomain will be directly sent to the destination web server.
It is important to make sure that all records for our domain and subdomains are entered in this step, including MX (mail) and other record types, because CloudFlare will become the DNS server for our domain.
FTP and SSH subdomains with CloudFlare disabled are added by default to the DNS configuration, to allow direct access to our server where the website is hosted.
Once we are satisfied that the full DNS configuration for our domain has been added, we can click on “I’ve added all missing records, continue”.
Choosing a plan for the CloudFlare Service
On the next screen, you will be asked to choose a plan, performance and security settings.
On the next and the final step of your setup, CloudFlare will ask you to update your nameservers. You will be asked to change your name servers and point them to CloudFlare nameservers.
Changing Name Servers to CloudFlare
The exact procedure to change the name servers to point to CloudFlare depends on the hosting provider used for your site. In most cases, your hosting provider will provide cPanel to manage your account. In case you have registered your domain directly with a domain registrar (goDaddy, EuroDNS,…) you will need to log into the registrar website to perform this change.
Once the name servers have been changed, you need to go back to CloudFlare set up page, and click on the button labeled ‘I’ve updated my nameservers, continue’ to finish the setup.
WordPress specific configuration for CloudFlare
The CDN configuration procedure explained so far is generic for all types of websites.
Sites using WordPress as CMS can additionally make use of a specific plugin to fully integrate CloudFlare with the site.
The main features of this plugin, according to the introductory page at the CloudFlare blog, are:
- Restores the original visitor IP to your WordPress logs. Before the plugin the IP addresses would reflect CloudFlare’s proxy IP addresses, which confused many blog owners.
- Helps optimize your server database.
- Report spam in the plugin, which then feeds the report to CloudFlare to make CloudFlare’s system smarter at stopping spam for sites on our network
WordPress sites using W3TC
W3 Total Cache is probably the most popular plugin among those used to improve the performace of sites using WordPress as CMS.
W3TC comes with its own integration with CloudFlare. WordPress sites using W3TC are not required to install the CloudFlare plugin.