Full Site Cache Enabler for KeyCDN

Make all HTML page on CDN cache, optimize your website speed. Make your site superfast.

Author:ZE3kr (profile at wordpress.org)
WordPress version required:4.4
WordPress version tested:4.6.18
Plugin version:2.2.1
Added to WordPress repository:16-02-2016
Last updated:17-03-2017
Warning! This plugin has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.
Rating, %:88
Rated by:7
Plugin URI:https://wordpress.org/plugins/full-site-cache...
Total downloads:4 972
Active installs:20+
plugin download
Click to start download

NOTE: This is an unofficial plugin.

This plugin can help you to use KeyCDN on your WordPress, not only your Media and CSS, but also all HTML page. It is much faster than other cache plugins because it can cache the content on the KeyCDN’s Edge Servers, which are close to the end-user. This plugins can automatically purge the page (and also the homepage, archive page, tag page, category page, feed, sitemap) when you publish a page or post.

Video – install and enable it within 3 minutes:

The website of this plugin (also is an example website using this plugin): keycdn.tlo.one

This plugin only cache the content on the KeyCDN’s Edge Servers but not on your origin server, so if you install another cache plugin and use both of them, can improve performance. Now this plugin only works well with Cache Enabler – WordPress Cache.

Contribute to this plugin

The author @ze3kr is not using KeyCDN and this plugin anymore, so it’s hard for @ze3kr to maintain this plugin. If any developer wants to contribute to this plugin, please leave your message here.

Features

This plugin support those features even if your server doesn’t support it, works well with shared hosting.

  • Faster than other caching plugins
  • SSL (HTTPS) with Let’s Encrypt FREE SSL
  • HTTP/2 with HPACK
  • GZIP
  • CDN (Layer 7 proxy)
  • Hide origin IP and prevent DDOS attack to your server (Suggest using CloudFlare for the wp-admin doamin, and change your IP address, see it in “IP White List” section).

Compare it with CloudFlare

The CloudFlare also can give you CDN, SSL and HTTP/2 support, but there’s something KeyCDN can but CloudFlare free plan cannot:

  • Cache HTML page, that means all the HTML page on CloudFlare is not cacheable and the request will bypass your origin server.
  • Use custom SSL certificate, including EV certificate.
  • Use CNAME and doesn’t need to change NS server.
  • Raw log forwarding in real time (CloudFlare has 24 hours delay for free plan)
  • Clear Cache by Tag

However, KeyCDN is not a free service but a pay-as-you-go service, KeyCDN is a affordable choice. And CloudFlare is also a very good DNS provider, you can still use it.

Requirements

  • PHP 5.4+ (PHP 5.6+ is recommended)
  • WordPress 4.4+
  • Disable all others cache plugin and use Cache Enabler instead (Optional).
  • KeyCDN account, sign up by this link and get 250GB of free traffic.

Manual Setup

You can set it up manually, and use an existing Zone, only if you want to manual setup, you need to do this.

Note: This plugin doesn’t support root domain like example.com, you have to change settings before use it.

1. Setup this plugin

Add the configuration code to your wp-config.php file in wp-config.php above the line reading /* That’s all, stop editing! Happy blogging. */.

You can get those configuration in the settings page of this plugin by click “Generate Configuration (for Manual Setup)” button.

Example configuration:

$fsckeycdn_useHTTPS = false; // Change it to true if your server support HTTPS.
$fsckeycdn_x_pull_key = 'KeyCDN'; // By default is `KeyCDN`, to make your server more secure, change it to a random 15 alphameric key, and update KeyCDN Zone settings.
$fsckeycdn_apikey = 'vHlnpHcE6GPEWyTWWisr4hE9e80Xvr4a'; // You can find your key at here https://app.keycdn.com/users/authSettings
$fsckeycdn_id = [
    1 => 10001,
    2 => 10002,
    3 => 10003,
]; // The key (1, 2, 3) is blog id. the value (10001, 10002, 10003) is KeyCDN Zone ID.
// $fsckeycdn_id = "10001"; // Use this line instead of above if you doesn't use multisite.

Then add a require_once function just below the variables just add, that need to run before evey plugin, to identify the server is KeyCDN or not.

require_once(ABSPATH . 'wp-content/plugins/full-site-cache-kc/include.php'); // This plugin need run some scripts before everything, so you need to add this, if you use a different location for plugins, change it.

After that, you can enable this plugin.

2. Setup DNS and Change Siteurl

You need to use a domain like www.example.com or blog.example.com but not root domain like example.com, because you need to create a CNAME on that domain.

If you are using domain like www.example.com, you need to create a DNS at wp-admin.example.com that point to your server.

If you are using domain like blog.example.com, you need to create a DNS at wp-admin-blog.example.com that point to your server.

Example BIND DNS file for root domain:

example.com 300 IN A <origin-server>
www.example.com 300 IN CNAME <your-zone-name>-<userid-hex>.kxcdn.com
wp-admin.example.com 300 IN A <origin-server>

After you setted up, you need to go to the new host to visit WordPress dashboard.

And you need to change Siteurl to the new domain, keep Home URL not change. Example: Siteurl is wp-admin.example.com and Homeurl is www.example.com. Or Siteurl is wp-admin-blog.example.com and Homeurl is blog.example.com.

And if you need to edit your post, or go to the dashboard, you need to goto wp-admin.example.com/wp-admin/ or wp-admin-blog.example.com/wp-admin/

3. Setup KeyCDN

You need to add a Pull Zone in KeyCDN, and set Origin URL to http(s)://wp-admin.example.com, set SSL to Letsencrypt (You can get free SSL support!), enable Cache Cookies, Strip Cookies and Forward Host Header. Set Expire (in minutes) to 0 and set Max Expire (in minutes) to 1440 or bigger, and make sure to disable CORS.

Advance Options

Default Zone Options

You can add $fsckeycdn_default_settings variable as a array in wp-config.php to replace the default value of KeyCDN Zone.

Default value:

$fsckeycdn_default_settings = [
    'status' => 'active',
    'type' => 'pull',
    'forcedownload' => 'disabled',
    'cors' => 'disabled',
    'gzip' => 'enabled',
    'expire' => '0',
    'http2' => 'enabled',
    'securetoken' => 'disabled',
    'cacheignorecachecontrol' => 'enabled',
    'cacheignorequerystring' => 'disabled',
    'cachestripcookies' => 'enabled',
    'cachecanonical' => 'disabled',
    'cacherobots' => 'disabled',
    'cachehostheader' => 'enabled',
    'cachecookies' => 'enabled',
    'cachestripcookies' => 'enabled',
];

You can see all the parameters that can set.

Example:

$fsckeycdn_default_settings = [
    'cachemaxexpire' => '525949'.
    'sslcert' => 'letsencrypt',
]

Different API Key for Each Site

KeyCDN has Zone Limit, so if your has too many site in your multisite, you have to use it.

This plugin can use different API Key for each site:

$fsckeycdn_apikey = [
    1 => 'vHlnpHcE6GPEWyTWWisr4hE9e80Xvr4a',
    2 => 'w752ikcfTv0srgLhwNHayBpxAss8VT3X',
    3 => 'BI0lFsGBKOF5S6Y0Kc9zaQkq8BQXVJsv',
];

And you can use different API Key for every 10 site like:

$fsckeycdn_apikey = [
    '0X' => 'vHlnpHcE6GPEWyTWWisr4hE9e80Xvr4a',
    '1X' => 'w752ikcfTv0srgLhwNHayBpxAss8VT3X',
    '2X' => 'BI0lFsGBKOF5S6Y0Kc9zaQkq8BQXVJsv',
];

0X for site 1~9, 1X for site 10~19.

Variable X-Pull Key

By default online setup will enable this feature, this make X-Pull Key more secure, especially for multisite.

This feature can generate different X-Pull Key for each domain, even if you enable Forward Host Header, it can prevent visitor visit your site using another domain.

Algorithm:

substr(fsckeycdn_convert('f'.md5($fsckeycdn_x_pull_key.$_SERVER['HTTP_HOST'])),-15);

“fsckeycdn_convert” is a function that can convert hexadecimal to alphameric.

function fsckeycdn_convert($s, $to=62) {
    $dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $res = '';
    $b = '';
    if($to > 0) {
        $to = ceil(log($to, 2));
        for($i=0; $i<strlen($s); $i++) $b .= sprintf('%04b', hexdec($s{$i}));
        while(strlen($b) >= $to) {
            $res = $dict{bindec(substr($b, -$to))} . $res;
            $b = substr($b, 0, -$to);
        }
        $res = $dict{bindec($b)} . $res;
        return $res;
    }
    $to = ceil(log(-$to, 2));
    for($i=0; $i<strlen($s); $i++) $b .= sprintf("%0{$to}b", strpos($dict, $s{$i}));
    while(strlen($b) > 4) {
        $res = $dict{bindec(substr($b, -4))} . $res;
        $b = substr($b, 0, -4);
    }
    if(bindec($b)) $res = $dict{bindec($b)} . $res;
    return $res;
}

Custom CDN Domain

You can custom your CDN domain, it’s a specific domain that only for Images, CSS and JS.

Just add this in the wp-config.php.

$fsckeycdn_cdn_domain = 'cdn.example.com';<h3>Extra Settings For CloudFlare</h3>

You need to do some Extra Settings For CloudFlare after you actived KeyCDN. If you are using CloudFlare and KeyCDN, you might set your DNS like that, [CF] means this domain enabled CloudFlare Proxy, and others means DNS only. Note: You need to disable CloudFlare on KeyCDN domain to ensure make HTML cache available:

example.com 300 IN A <origin-server> [CF]
www.example.com 300 IN CNAME <your-zone-name>-<userid-hex>.kxcdn.com
wp-admin.example.com 300 IN A <origin-server> [CF]
[key].example.com 300 IN A <origin-server>

The key is a random value, keep it like your origin IP.

And add the configuration code to wp-config.php, and above require_once (Optional, use this feature can make sure even if others has your origin IP, the request direct to your origin server will reject by this plugin.):

$fsckeycdn_ip_white_list = [
    '103.21.244.0/22',
    '103.22.200.0/22',
    '103.31.4.0/22',
    '104.16.0.0/12',
    '108.162.192.0/18',
    '131.0.72.0/22',
    '141.101.64.0/18',
    '162.158.0.0/15',
    '172.64.0.0/13',
    '173.245.48.0/20',
    '188.114.96.0/20',
    '190.93.240.0/20',
    '197.234.240.0/22',
    '198.41.128.0/17',
    '199.27.128.0/21',
]; // This is CloudFlare's IP.
$fsckeycdn_ipv6_white_list = [
    '2400:cb00::/32',
    '2405:8100::/32',
    '2405:b500::/32',
    '2606:4700::/32',
    '2803:f800::/32'
]; // This is CloudFlare's IPv6.
$fsckeycdn_client_ip = $_SERVER['REMOTE_ADDR']; // The IP for CloudFlare, if it doesn’t work, please try `end(explode(', ',$_SERVER['HTTP_X_FORWARDED_FOR']))`
$fsckeycdn_client_real_ip = $_SERVER['HTTP_CF_CONNECTING_IP']; // The IP for real client (Optional)

Then, go to KeyCDN Zone settings page, change “Origin URL” to http(s)://[key].example.com, disabled “Origin Shield” and enable “Forward Host Header”.

About Purge

This plugin will purge all page that need to be purge, but when your customized your theme or changed your theme, you need to purge your site manually. go to the settings page and you can find purge button.

You can set $fsckeycdn_purge to change purge type.

  • 1. Purge that page and every archive page when a post/page published (Recommend, default).
  • 2. Purge whole blog (not include css, js and image) when a post/page published.
  • 3. Purge all page that need to be purge (Beta).
  • 4. Only purge that page when a post/page published.
  • 5. Do nothing when a post/page published.

Example:

$fsckeycdn_purge = 3;<h3>About “Cache Enabler” plugin</h3>

This plugin only cache the content on the KeyCDN’s Edge Servers but not on your origin server, so if you install another cache plugin and use both of them, can improve performance. Now this plugin only works well with Cache Enabler – WordPress Cache.

If you click “Clear Cache” on the admin bar, it will automatically clear KeyCDN Tag and also clear the cache of “Cache Enabler” plugin.

And if you change “Cache Behavior” in the settings page of “Cache Enabler”, it will also effect WP KeyCDN cache behavior.

Extra Settings For Root Domain

You will get an error if you trying to enable it for root domain, whatever you setup manually or not.

To solve this problem, you need to change your domain beginning with www., for example, if your blog domain is example.com, you have to change it to www.example.com.

However, you are still able to use root domain if your DNS provider supports ANAME record (or CNAME Flattening), for example, CloudFlare supports this feature, so you can set a CNAME record on your root domain and won’t get any error! The version 2.2.0 brings this feature, you need to add this line to your wp-config.php:

`

$fsckeycdn_root_domain_setup = true;
`

For NOT Multisite Installed

Go to general settings page, change WordPress Address and Site Address to the new domain.

For Multisite Installed

If you NOT use subdomain install, pleace see this page to find out how to move WordPress multisite to a new URL.

If you use subdomain install, I suggest you create a new blog and that blog’s domain is beginning with www., for example, your have a network blog which use domain example.com, and you create a another blog which use domain www.example.com. Then input the content from the old blog which use domain example.com, and enable this plugin for the new blog.

After you add require_once function in wp-config.php, it will automatically redirect the old blog to new bog, and not effect with admin page.

How to fully disable this plugin

It will disable KeyCDN for ALL blogs if you are using Multisite.

1. Remove the configurations

You need to remove the lines that you added in wp-config.php file. And you MUST remove these line before your delete this plugin.

/* Start WP KeyCDN code */
Some codes……
/* End WP KeyCDN code */

2. Delete this plugin

It’s optional.

3. Change the Siteurl

Use Search Replace DB, replace ://wp-admin. to ://www., and ://wp-admin- to ://.

How to disable A blog in Multisite

Just add this codes to wp-config.php

$fsckeycdn_id[1] = false;

You need to add this line after the $fsckeycdn_id if you use Manual Setup. Change 1 to your blog ID that need to disabled.

Known Issues

  • If you set a password to a post/page, it may cannot view by visitors even if they has password.