Website construction

Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate

Jager, June 2, 2019 · · 13929 times read

My previous post on my blog mentioned that Changed the personalized domain name Due to the inability to record in zhang.ge, the server has to be migrated to foreign countries. By the way, the blog currently uses AWS's lightsail, which coordinates Tokyo, and the configuration is shown in the figure below:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 1: I chose Package 3, with a 20GB cloud data disk attached

A little more than $10 per month, and about 80 soft sister coins are converted, which is more cost-effective than the vast majority of domestic cloud servers configured in Hong Kong or overseas. It will be cheaper if you choose the first two packages.

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 2: Expense report in recent months

However, friends who have really used foreign servers know that under the influence of "Kung Fu King", foreign servers will twitch in the evening. So consider building a layer CDN To alleviate, and may also help carry malicious attacks.

What is CloudFlare (hereinafter referred to as CF) and how to set it will not be repeated here. You can search for the detailed use tutorial of CF online, and many netizens and bloggers have shared the tutorial with pictures and texts. The following is mainly to record and share some experiences or skills found by Zhang Ge's blog in the use of CF, hoping to provide some references for students who are using or will use CF.

1、 CNAME access mode

Those who have known about CF may know that the CF free package can only be accessed through NS. This method is not flexible enough and takes effect slowly. The most important problem is that it is impossible to manually resolve the CF node IP address.

It is also very simple to realize the CNAME access mode of CF. Here we can use a third-party CF management platform. For example, Zhang Ge Blog uses: BNXB These platforms operate domain names through CF's API, so they have localized all CF's functional options, greatly reducing the threshold for domestic users! Of course, the most attractive thing is that I can use CNAME access.

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 3: BNXB settings

Access is very simple. You can perform access operations on the platform by registering the platform account and referring to the prompt to bind the CF API account. Here you can directly select the CNAME access method:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 4: CNAME Access

If you find it hard to understand the description and do not know how to fill it in, you can actually select the DNS access method first, and then convert it to the CNAME method again on the platform. This platform can switch between CNAME and DNS access modes at any time:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 5: Access mode conversion

After the access is completed, we can refer to some experiences shared by previous people on the network to optimize the configuration (all in Chinese, I will not introduce them here), and the platform will provide a CNAME record available:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 6: Resolution record interface, CNAME record

At this time, we can resolve the previous @ or WWW records to this cname in the original domain name management (such as dnspod, HiChina, etc.), and then complete the CF access.

2、 Specify node resolution

As shown at the end of Figure 6 above, after accessing the CF, the CF will assign an entry IP to the domain name, usually from Los Angeles, West America. This IP is actually the AnyCast technology. When users access it, they will allocate a real node IP nearby to speed up access. Therefore, the IP detected by the ping tool is only the AnyCast entry IP, which does not show the actual node IP address.

However, the method to view the real node IP is also very simple. Each CF node has a trace interface, and the access method is: http://{Node IP/domain name}/cdn cgi/trace. For example, to view the real node address of Zhang Ge's blog, visit https://zhang.ge/cdn-cgi/trace

Through actual measurement, it is found that at present, domestic users will visit the US West node by default, and only the mobile and education network will visit the Hong Kong node. Therefore, the effect of domestic use of CF is still not optimistic. In this way, there will be some students' imaginations. Since I can find the real nodes, can I resolve to some fast nodes? For example, Hong Kong node. The answer is yes!!

With the above CNAME access prerequisites, we can resolve to the node IP anywhere in the domain name resolution. On the contrary, using NS access can not be so arbitrary.

The specific node IP of CF will not be announced here. If you are interested, go to Google and Baidu to search.

As a result, the two major players, China Telecom and China Unicom, were originally traveling slowly in the West, but now they can go to two relatively high-speed nodes, and the speed has been greatly improved! The default line uses the AnyCast entrance IP assigned by CF by default. First, the mobile operator users can use the Hong Kong line, and second, it is for the sake of bottom line. Speaking of the bottom line, I have to share another technique for using dnspod - D monitoring:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 8: Analysis of D monitoring automatic switching

As shown in the figure, the D monitoring function of dnspod can detect whether the site is available. If not, it can automatically switch to the available IP. So in Figure 7, I set two IPs for both Telecom and Unicom (two free packages at most). In other words, when D monitoring finds that the CF node specified manually is down, it can automatically switch to the other two IPs (including the default AnyCast IP), which adds another layer of security:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate

3、 Force Cache Configuration

When I access CF, I find that although I set cache in cache and other configurations, the page is not cached CDN Node. Finally, through the help document on the official website, it is found that CF does not cache html pages by default( Document address ), and you need to set custom Page Rules, which is exactly the same as Baidu Cloud Acceleration's custom rules! Therefore, Baidu Cloud Acceleration is actually designed by "learning from" CF.

Since they are exactly the same, you can refer to what I shared earlier Baidu Cloud Acceleration Custom Rule Settings To set the Page Rules of CF, as shown in the figure:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 10: Page rules setting

Just like Baidu Cloud Acceleration (or Baidu Cloud Acceleration is the same as CF), the free package can only set three rules, so if we are unwilling to spend money, we can only set it as shown in the figure: except that the background and comment pages are not cached, everything else is cached. As shown in the figure, there is another key point, Edge Cache TTL This option must be set, otherwise it will not be cached!

Although it is somewhat extensive, it is acceptable for personal blogs. It may not be satisfied unless some special pages are also required not to cache. Of course, if you really need more rules, you can also spend 5 dollars/month to buy 5 additional rules:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 11: Pay to purchase Page Rules entries

In addition, I can see from the CF help document that Page Rules can actually be configured according to cookies. I didn't find it at that time, but it should be a paid version. It is unknown whether the five rules for purchasing for $5 can be configured more flexibly through cookies. The description is as follows:

How do you specify cookie names for Bypass Cache Cookie or Cache on Cookie?
Note that Bypass Cache on Cookie is available only on Business and Enterprise plans & Cache on Cookie is Enterprise only.
You can specify multiple cookie names by using the OR “|” operator. For example, to match  cookie1 cookie2 , or  cookie3 , you would write:  cookie1|cookie2|cookie3 . You can also use the wildcard “.*” operator, i.e.  cookie1.* .

If it can be configured through cookies, it is very powerful, such as the WordPress specific configuration

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 12: Configuration of WordPress dedicated html cache page officially shared by CF

4、 WordPress plug-in

After using CDN, many friends may be troubled by caching, such as content updates, but the foreground can't see the latest content because of the CDN cache, which can be described as happy caching, boring caching. Therefore, every time I use a new CDN, I will write some supporting cache auto cleaning code (I have never used it, and naturally I won't spend time researching it~~), such as sharing before:

This time, it is no exception. And CF obviously has a more mature API interface. When I was going to roll it up, I found a new continent! In fact, CF has a dedicated WordPress plug-in called Cloudflare. We just need to search and install the background plug-in interface:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 13: Search and install CloudFlare plug-in

After installation, log in to the CF account in the plug-in setting interface to get a simple version of the CF control background:

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 14: Cloudflare plug-in settings
 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 15: Cloudflare background data statistics

Of course, these functions can be configured on official or third-party platforms, which can only be said to be icing on the cake. What we are most concerned about is that the CDN cache can be automatically deleted when the content is updated. Does the plug-in support it? The answer is yes!

 Zhang Ge Blog shares experience and skills of using CloudFlare CDN to accelerate
Figure 16: Automatically update cache settings

In this way, I don't need to read the code, and you don't need to toss the code! It's really refreshing!

5、 Please look forward to

So much for the moment. If I find more practical CF configuration skills later, I will continue to update them to this article!

1 response
  1. aunsen 2020-6-11 · 21:29

    It seems that my cc domain name can not be filed, and the Hong Kong host is less troublesome. PS: You are still so fond of tossing

    • user 2020-7-27 · 1:28

      Cc is now operating on the north bank

  2. Love laughing 2020-8-16 · 16:45

    Come to watch and learn. I don't know how to set CDN

  3. loop 2020-8-17 · 17:25

    Blogger, namesilo cannot add CNAME record of bare domain name

  4. raymond 2020-10-5 · 23:11

    Could you help me make this optional node? I've been looking for each other for a long time, but no one can do it. Penguin: 1423037, six four five

  5. Buddhist software 2021-3-27 · 1:57

    How to deal with firewall rules

  6. Buddhist software 2021-3-28 · 16:58

    Why do I have three IPs, two CDNs, one origin station, and D monitor my settings without modification

  7. Sky Games 2021-4-1 · 18:59

    Cf Isn't the speed not good in mainland China, and can it be improved,

  8. leo 2021-6-24 · 6:49

    Since you use the external vps, why not try Oracle for free, which is several times better than your configuration. I have been doing it for free.

  9. leo 2021-6-24 · 6:52

    Can the blogger say what the theme is? It's like mine. I don't know if it's the same person

  10. Buddhist software 2021-6-25 · 11:58

    Why can I completely overwrite the source station IP after adding a CDN before, but now there are still more than a dozen nodes not covered after deleting and re adding

  11. pwndrer4 2021-12-24 · 20:39

    Please let me know how to cache the reviewers' cookies on the comment page after using cloudflare to enable caching, how to remove them, and whether to give a tutorial

  12. Google Optimization 2022-12-7 · 17:39

    Do you have any other useful accelerator recommendations

  13. I'm also using CloudFlare, which is really good!

  14. Wang Guangwei's Blog 2024-1-9 · 19:39

    This function is actually very easy to use, and it's good to do cross-border e-commerce