Thêm nhiều site vào Cloudflare bằng automation Add multiple sites via automation
Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page.
← Danh mục ← CatalogGiải thích nhanh Quick context
Tutorial đa lĩnh vực — kết hợp nhiều sản phẩm Cloudflare. Tutorial «Thêm nhiều site via automation» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Docs gốc chia khoảng 2 bước chính; bản tóm tắt dưới đây giúp bạn nắm khung trước khi làm theo từng lệnh.
To add multiple sites to Cloudflare at once and more efficiently, you can do so via the Cloudflare API.
Lưu ý trước khi làm Notes before you start
- Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức.
- Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ.
- Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production.
Tài liệu gốc — rà soát lần cuối: 5 months ago Official docs — last reviewed: 5 months ago
Tổng quan Overview
Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
To add multiple sites to Cloudflare at once and more efficiently, you can do so via the Cloudflare API.
Adding multiple sites can be useful when you:
- Have multiple domains mapping back to a single, canonical domain (common for domains in different countries - such as
.com.au,.co.uk\- that you want protected by Cloudflare). - Are a partner ↗, agency, or IT consultancy, and manage multiple domains on behalf of your customers.
- Are moving an existing set of sites over to Cloudflare.
Using the API will allow you to add multiple sites quickly and efficiently, especially if you are already familiar with how to change your nameservers or add a DNS record.
This tutorial assumes domains will be added using a primary DNS setup (full).
---
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Yêu cầu trước Prerequisites
Phần «Yêu cầu trước» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
To add multiple sites to Cloudflare via automation, you need:
- An existing Cloudflare account.
- Command line with
curl - A Cloudflare API token with one of the following permissions:
Zone-level Administrator Zone-level Zone: Edit and DNS: Edit * Account-level Domain Administrator
- To have disabled DNSSEC for each domain at your registrar (where you bought your domain name).
Provider-specific DNSSEC instructions This is not an exhaustive list, but the following links may be helpful: DNSimple ↗ Domaindiscount24 ↗ DreamHost ↗ Dynadot ↗ Enom ↗ Gandi ↗ GoDaddy ↗ Hostinger ↗ Hover ↗ Infomaniak ↗ InMotion Hosting ↗ INWX ↗ Joker.com ↗ Name.com ↗ Namecheap ↗ NameISP ↗ Namesilo ↗ OVH ↗ Squarespace ↗ Registro.br ↗ Porkbun ↗ (do not fill out keyData) TransIP ↗ Note If your previous provider allows you to add DNSKEY records on the zone apex and use these records in responses to DNS queries, refer to this migration tutorial to learn how to migrate a zone with DNSSEC enabled.
---
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
1. Thêm domains 1. Add domains
Phần «Thêm domains» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
- Create a list of domains you want to add, each on a separate line (newline separated), stored in a file such as
domains.txt. - Create a bash script
add-multiple-zones.shand add the following. Adddomains.txtto the same directory or update its path accordingly.
Terminal window
for domain in $(cat domains.txt); do
printf "Adding ${domain}:\n"
curl https://api.cloudflare.com/client/v4/zones \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"account": {
"id":"<ACCOUNT_ID>"
},
"name": "'"$domain"'",
"type": "full"
}'
printf "\n\n"
done - Open the command line and run:
Terminal window
bash add-multiple-zones.sh Warning
There are limitations on the number of domains you can add at a time. Refer to limitations for details.
After adding a domain, it will be in a Pending Nameserver Update state.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Additional options Additional options
Phần «Additional options» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
jq ↗ is a command-line tool that parses and beautifies JSON outputs.
This tool is a requirement to complete any additional option steps in this tutorial.
Terminal window
echo '{"foo":{"bar":"foo","testing":"hello"}}' | jq . Refer to jq documentation ↗ for more information.
Cloudflare offers a quick scan that helps populate a zone's DNS records. This scan is a best effort attempt based on a predefined list of commonly used record names and types.
This API call requires the domain ID. This can be found in the following locations:
Using jq with the first option above, modify your script add-multiple-zones.sh to extract the domain ID and run a subsequent API call to quick scan DNS records.
JavaScript
for domain in $(cat domains.txt); do
printf "Adding ${domain}:\n"
add_output=`curl https://api.cloudflare.com/client/v4/zones \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"account": {
"id":"<ACCOUNT_ID>"
},
"name": "'"$domain"'",
"type": "full"
}'`
echo $add_output | jq .
domain_id=`echo $add_output | jq -r .result.id`
printf "\n\n"
printf "DNS quick scanning ${domain}:\n"
scan_output=`curl --request POST https://api.cloudflare.com/client/v4/zones/$domain_id/dns_records/scan \
--header "X-Auth-Email: <EMAIL>" \
--header "X-Auth-Key: <API_KEY>"`
echo $scan_output | jq .
done Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
2. Cập nhật nameservers 2. Update nameservers
Phần «Cập nhật nameservers» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
For each domain to become active on Cloudflare, it must be activated in either Full setup or Partial setup. The following script will output a list containing the nameservers associated with each domain.
You can find your zones nameservers in the following locations:
- Modify your script
add-multiple-zones.shto print a CSV with data from theCreate ZoneJSON response.
JavaScript
for domain in $(cat domains.txt); do
printf "Adding ${domain}:\n"
add_output=`curl https://api.cloudflare.com/client/v4/zones \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"account": {
"id": "<ACCOUNT_ID>"
},
"name": "'"$domain"'",
"type": "full"
}'`
# Create csv of nameservers
echo $add_output | jq -r '[.result.name,.result.id,.result.name_servers[]] | @csv' >> /tmp/domain_nameservers.csv
domain_id=`echo $add_output | jq -r .result.id`
printf "\n\n"
printf "DNS quick scanning ${domain}:\n"
scan_output=`curl --request POST https://api.cloudflare.com/client/v4/zones/$domain_id/dns_records/scan \
--header "X-Auth-Email: <EMAIL>" \
--header "X-Auth-Key: <API_KEY>"`
echo $scan_output | jq .
done
printf "name_servers are saved in /tmp/domain_nameservers"
cat /tmp/domain_nameservers.csv | ID | ZONE | NAME SERVERS | | ---------- | ----------- | --------------------------------------------- | | <ZONE\_ID> | example.com | arya.ns.cloudflare.com, tim.ns.cloudflare.com |
- Use the values in the NAME SERVERS column to update the nameservers at the registrar of each domain.
---
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Limitations Limitations
Phần «Limitations» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
There are limitations on the number of domains you can add at a time - specifically, you can only sign up a maximum of 25 domains every 10 minutes.
In addition, if you have over 50 domains and, of those domains, more are pending than active, you will be blocked from adding more. We recommend waiting until your pending sites have been activated before adding more.
Common issues Common issues
Phần «Common issues» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
If any errors were returned in this process, the domain may not be registered (or only just registered), be a subdomain, or be otherwise invalid. For more details, refer to Cannot add domain.
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/fundamentals/","name":"Cloudflare Fundamentals"}},{"@type":"ListItem","position":3,"item":{"@id":"/fundamentals/manage-domains/","name":"Domains"}},{"@type":"ListItem","position":4,"item":{"@id":"/fundamentals/manage-domains/add-multiple-sites-automation/","name":"Add multiple sites via automation"}}]} Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs).
Tài liệu gốc ↗ Official docs ↗