Skip to main content
Currently using’s CDN Solution from:https://www.eallion.com/cdn-cname-cloudflare/ Take a look at the CDN structure diagram. Our goal is to solve the global CDN problem at a low cost. cdn_dns Previous articleBackup

Introduction

domestic and overseas traffic splitting,not only reduces costs,but also improves website performance,optimizes TTFB。 I don’t remember when I started having such thoughts, so I went to search for it. It turns out that the tutorials online are quite old. Where most of the talk’s is access to CNAME is through Cloudflare for SaaS, but it doesn’t support regular domain names, and can’t access R2 or Workers. In terms of function’s priority, I need the partition resolution function most, which results in the inability to transfer the domain name’s NS to Cloudflare. Cloudflare’s DNS is indeed excellent, but Cloudflare doesn’t support geo-based resolution, it has CNAME flattening, but it will resolve all mainland China’s IPs to China Unicom. On the contrary, domestic DNS service providers do a good job in partition resolution, maybe because domestic domain names need this function more. Can’t fully commit, but want to use Cloudflare, so have to use some tricks. In March 2022, Cloudflare announced changes to Cloudflare for SaaS’s charging strategy. Each account can have 100 free domain names, and charges $0.1/month per extra domain. We use Cloudflare for SaaS to connect domains via CNAME and enjoy Cloudflare’s powerful edge calculation capabilities. Yes for small websites, such as this blogs, the above services are free’s, free quota:
  • DNSPod: Use’s professional version, but freeversions also has partition resolution
  • Tencent Cloud COS: 50G/month; 2 million requests
  • Tencent Cloud CDN: 10G/month
  • Cloudflare CDN: No upper limit for normal use
  • Cloudflare R2: 10G/month; 1 million/10 million requests
  • Backblaze B2: 10G/month; has [Traffic Alliance] with Cloudflare(https://www.backblaze.com/cloud-storage/integrations)
I skip Tencent Cloud’s Configuration and only talk about Cloudflare’s part here. The prerequisite is that you already have an available domain name in your Cloudflare account. This domain name is used to provide 回退源 (Fallback Origin), assuming this domain name is example.com.

Create R2 and bind a custom domain name

  1. Log in to the control panel: https://dash.cloudflare.com/, Cloudflare supportsChinese;
  2. Create R2 bucket’s Method is skipped here, for example, create: r2-blog-test;
  3. in R2 设置 公开Visit 自Definition域 连接域 Add a custom domain name to the ‘s R2 you just created:
/assets/images/posts/2023/07/r2_custom_hostname.png Then the domain name’s DNS will automatically appear with a resolution: /assets/images/posts/2023/07/custom_hostname_dns.png

Subscribe to Cloudflare for SaaS

  1. Select the domain name example.com in Zones;
  2. Select 自Definition主机名 in the domain name’s SSL/TLS;
  3. Select Enable Subscription. Subscriptions can be made using Paypal.
/assets/images/posts/2023/07/enable_cloudflare_saas.png

Add custom domain name

After the subscription is successful, first add 回退源:images.example.com. This return-to-origin domain name is bound in R2’s self-defined domain name. /assets/images/posts/2023/07/cf_callback_hostname.png Then click 添加自Definition主机名 and fill in the CDN domain name, such as images.eallion.com. The recommended validation method is TXT Validation. /assets/images/posts/2023/07/add_custom_hostname.png After adding, you need to validate the domain name. Go to your own domain name resolution console, such as DNSPod, and add 2 TXT records. Wait for both 证书State and 主机名State to become 有效. /assets/images/posts/2023/07/cf_dns_txt_records.png

Parse CNAME

After 回退源State 证书State and 主机名State become 有效, go to your own domain name resolution console to add CNAME resolution. Point the production environment’s images.eallion.com CNAME to images.example.com. /assets/images/posts/2023/07/dns_cname_records.png Normally’s tutorial ends here. But this way isVisit doesn’t work with R2’s resources. The most important step, use Worker Proxy R2.

Create new Worker Proxy R2

There is official documentation on how to pass Worker Visit R2: Use R2 from Workers:https://developers.cloudflare.com/r2/api/workers/workers-api-usage/ Just follow the documentation tutorial step by step. If you are lazy, you don’t want to authenticate. Then just use my streamlined code. The DELETE and PUT ‘s codes are directly removed, leaving only GET. Workers can be created manually in the background without Wrangle CLI scripts. Switch to the Worker 和 Pages column on the left, 创建应用程序, just give it a name, choose a template Deployment, and change the code later. Click 快速edit to copy the following code to worker.js, save and Deployment:
Returned after Deployment is successful. In the current Worker ‘s settings, Variable R2 存储桶绑定 add binding:
  • VariablenameMY_BUCKET
  • R2 存储桶: Select yes should’s bucket
/assets/images/posts/2023/07/r2_binding.png

Workers routing

Go back to Zones, select the domain name, and add Workers routing:
  • 路由: Be sure to fill in the production environment’s domain name, do not fill in Cloudflare’s source domain name, such as: images.eallion.com/*;
  • Worker: Select the previous step to create’s Worker;
  • 环境:Production.
/assets/images/posts/2023/07/r2_worker_router.png At this point, you should be able to visit Cloudflare R2’s content using CNAME’s method.

Worker Proxy Backblaze B2

In fact, R2 is enough, but B2 may be needed for various reasons. Actually it’s pretty much the same. Backblaze also has official documentation introducing how to visit B2 through Cloudflare Worker. Docs: Integrate Cloudflare Workers with Backblaze B2 Let’s briefly introduce how to do it: (It is also recommended to read Official Docs.)
1. Create a new Cloudflare Worker, worker.js
2. Set Worker Environment Variables
  • ALLOW_LIST_BUCKET:true
  • B2_APPLICATION_KEY:K004WJZP11111111111111111111Q
  • B2_APPLICATION_KEY_ID:0042e9999999920000000001
  • B2_ENDPOINT:s3.us-west-004.backblazeb2.com
  • BUCKET_NAME:eallion-static
The APP KEY and ID need to be generated in the Backblaze backend, and B2_ENDPOINT needs to be viewed in its own B2 bucket.
3. Manually add CNAME to resolve to B2
/assets/images/posts/2023/07/b2_cf_record.png
  • 类型: Select CNAME
  • name: used for 回退源, such as: b2.example.com, fill in b2
  • 内容: Fill in your own B2 bucket allocation’s S3 URL, there’s a tutorial here written byis Friendly URL, it’s not necessary, there is one more step to reverse generation.
/assets/images/posts/2023/07/backblaze_url.png
4. Configuration fallback source
Zones’s domain name is Backblaze B2. Set’s CNAME name is whatever it is, then fill in whatever the fallback source is, such as: b2.example.com. Just refer to the previous article.
5. Configuration self-defined host name
Refer to the previous article.
6. Configuration Worker routing
  • 路由: Be sure to fill in the production environment’s domain name, do not fill in Cloudflare’s source domain name;
  • Worker: Select the previous step to create’s Worker;
  • 环境:Production.
Adding Worker routes for Backblaze B2 is different from Cloudflare R2. You need to add 2 items:
  • b2.example.com/* also needs to be added to the Worker route
  • images.eallion.com/*
::: CDN certificate automatic replacement The above has solved the basic issue, but there is still a pain point with the SSL certificate issue. CF offers free renewal and update of never-expired certificates, but domestic CDN manufacturers generally need to manually upload their own application’s certificates… This is really inconvenient, so I was wondering if I could find some solutions: ::: details Automatically update certificate scripts Cause: Because Configuration domestic CDN basically requires manual uploading of certificates, but the domain name certificate I applied for basically has a three-month shelf life, so I thought of writing an automatic script to automatically update the certificate. Environment: 1panel (domestic panel, automatic application for certificates, and other powerful functions, very convenient’s) I use Duojiyun as an example. Other manufacturers can find the corresponding SDK. The code is as follows:
The basic implementation idea is to first remove the existing certificate, then add the read’s certificate, then upload and activate the uploaded’s certificate, so that the Implementation CDN certificate is automatically configured. You can use 1panel to automatically execute scripts every one month to update the certificate. It’s really Implementation again a Qiweiwei’s tips ✊✊✊ :::
Last modified on April 17, 2026