Cloudflare 从接入域名到 Tunnel 部署:新手一篇上手教程
Cloudflare 新手入门指南|附完整命令
Cloudflare 是目前最常用的网站加速、安全防护与 DNS 托管平台之一。无论你是个人博客站长、独立开发者,还是中小型网站管理员,都可以通过 Cloudflare 快速获得 DNS 解析、CDN 加速、HTTPS 证书、DDoS 防护、Web 防火墙、Zero Trust 隧道等能力。
本文面向 Cloudflare 新手,尽量用清晰、可操作的方式,带你从注册账号、接入域名、配置 DNS、开启 HTTPS、设置缓存、安全防护,到使用 Cloudflare Tunnel 暴露本地服务。文中会附上常用命令,方便你直接复制使用。
一、Cloudflare 是什么?
Cloudflare 可以理解为位于用户和你的网站服务器之间的一层“智能代理网络”。
当用户访问你的网站时,流量会先进入 Cloudflare 全球节点,再由 Cloudflare 转发到你的源站服务器。这样做有几个好处:
-
DNS 解析更稳定 Cloudflare 提供免费的权威 DNS 托管服务,解析速度快,稳定性高。
-
网站访问更快 静态资源可以被缓存到 Cloudflare 全球 CDN 节点,用户访问时就近读取。
-
自动 HTTPS Cloudflare 可以为你的域名提供免费的 SSL/TLS 证书。
-
隐藏源站 IP 开启代理后,用户看到的是 Cloudflare 节点 IP,而不是你的服务器真实 IP。
-
抵御部分攻击 Cloudflare 默认具备一定的 DDoS 防护能力,也可以配置 WAF、防火墙规则、访问规则等。
-
无需公网 IP 暴露本地服务 通过 Cloudflare Tunnel,你可以把本地服务安全地暴露到公网域名下,而不需要开放路由器端口。
二、接入 Cloudflare 前需要准备什么?
在开始之前,你需要准备以下内容:
- 一个已经购买的域名,例如:
example.com - 一个 Cloudflare 账号
- 一个网站服务器,或者本地正在运行的服务
- 能够修改域名注册商处的 DNS 服务器地址
- 基本的命令行操作能力
如果你只是想先体验 Cloudflare 的 DNS 和 CDN,服务器可以是任意 VPS,例如:
- 阿里云 ECS
- 腾讯云 CVM
- 华为云服务器
- AWS EC2
- DigitalOcean
- Vultr
- Hetzner
- Oracle Cloud
- 本地 NAS 或家用服务器
三、注册 Cloudflare 账号
访问 Cloudflare 官网:
https://www.cloudflare.com/
点击右上角 Sign Up 注册账号。
注册完成后,登录 Cloudflare 控制台:
https://dash.cloudflare.com/
进入控制台后,点击:
Add a site
输入你的域名,例如:
example.com
注意,这里只需要输入根域名,不要输入 https://,也不要输入路径。
正确示例:
example.com
错误示例:
https://example.com
www.example.com/blog
四、选择 Cloudflare 套餐
Cloudflare 会让你选择套餐。
对于新手或普通个人网站,选择免费套餐即可:
Free Plan
免费套餐已经包含:
- DNS 托管
- 全球 CDN
- 免费 SSL/TLS
- 基础 DDoS 防护
- 页面规则
- 缓存设置
- 基础防火墙能力
除非你有企业级安全需求,或者需要更高级的 WAF、缓存规则、图片优化功能,否则免费版完全够用。
五、添加 DNS 解析记录
Cloudflare 会自动扫描你原来的 DNS 记录,但不一定准确。建议你手动检查。
进入 Cloudflare 后台:
Websites → 选择你的域名 → DNS → Records
常见 DNS 记录如下。
1. 根域名解析到服务器 IP
假设你的服务器 IP 是:
1.2.3.4
添加 A 记录:
| 类型 | 名称 | 内容 | 代理状态 |
|---|---|---|---|
| A | @ | 1.2.3.4 | Proxied |
其中:
@表示根域名,例如example.comProxied表示开启 Cloudflare 代理,图标是橙色云朵DNS only表示只做 DNS 解析,不走 Cloudflare CDN,图标是灰色云朵
2. www 子域名解析
如果你希望访问:
www.example.com
可以添加 CNAME 记录:
| 类型 | 名称 | 内容 | 代理状态 |
|---|---|---|---|
| CNAME | www | example.com | Proxied |
也可以使用 A 记录:
| 类型 | 名称 | 内容 | 代理状态 |
|---|---|---|---|
| A | www | 1.2.3.4 | Proxied |
一般推荐使用 CNAME 指向根域名,方便后续维护。
3. API 子域名解析
如果你的网站有 API 服务,例如:
api.example.com
可以添加:
| 类型 | 名称 | 内容 | 代理状态 |
|---|---|---|---|
| A | api | 1.2.3.4 | Proxied |
4. 邮箱解析记录
如果你使用企业邮箱,需要添加 MX、TXT、SPF、DKIM、DMARC 等记录。
示例:
| 类型 | 名称 | 内容 |
|---|---|---|
| MX | @ | mail.example.com |
| TXT | @ | v=spf1 include:example.com ~all |
| TXT | _dmarc | v=DMARC1; p=none; rua=mailto:admin@example.com |
需要注意:邮箱相关记录通常不要开启代理,因为 Cloudflare 的代理主要面向 HTTP/HTTPS 流量。
六、修改域名 NS 服务器
Cloudflare 添加站点后,会给你两个 nameserver,例如:
dora.ns.cloudflare.com
lars.ns.cloudflare.com
你需要登录你的域名注册商后台,把原来的 NS 服务器改成 Cloudflare 提供的两个。
常见域名注册商包括:
- 阿里云
- 腾讯云
- GoDaddy
- Namecheap
- Namesilo
- Porkbun
- Cloudflare Registrar
修改完成后,等待 DNS 生效。一般需要几分钟到数小时,最长可能 24 到 48 小时。
七、检查 DNS 是否已经生效
在本地电脑或服务器上,可以使用以下命令检查。
1. 查看域名 NS 记录
dig NS example.com
如果返回结果中出现 Cloudflare 的 nameserver,说明 NS 已经生效。
也可以使用:
nslookup -type=NS example.com
2. 查看 A 记录
dig A example.com
或者:
nslookup example.com
如果你在 Cloudflare 中开启了代理,查询结果通常会显示 Cloudflare 的 IP,而不是你的源站 IP。这是正常现象。
3. 查看完整 DNS 解析过程
dig +trace example.com
这个命令可以看到从根 DNS 到权威 DNS 的完整解析链路。
4. 使用指定 DNS 服务器查询
查询 Cloudflare 公共 DNS:
dig @1.1.1.1 example.com
查询 Google DNS:
dig @8.8.8.8 example.com
八、配置 SSL/TLS 加密
Cloudflare 中非常重要的一项配置是 SSL/TLS。
进入:
Websites → 选择域名 → SSL/TLS → Overview
你会看到几个模式:
-
Off 不启用 HTTPS,不推荐。
-
Flexible 用户到 Cloudflare 是 HTTPS,但 Cloudflare 到源站是 HTTP。不推荐长期使用,容易出现重定向循环。
-
Full 用户到 Cloudflare 是 HTTPS,Cloudflare 到源站也是 HTTPS,但不校验证书是否合法。
-
Full strict 用户到 Cloudflare 是 HTTPS,Cloudflare 到源站也是 HTTPS,并且校验证书有效性。推荐使用。
新手推荐配置:
Full strict
但前提是源站服务器上必须有有效证书。
九、源站安装 HTTPS 证书
源站服务器可以使用 Let’s Encrypt 免费证书,也可以使用 Cloudflare Origin Certificate。
方案一:使用 Certbot 安装 Let’s Encrypt 证书
以下以 Ubuntu / Debian 为例。
更新系统:
sudo apt update
sudo apt upgrade -y
安装 Certbot:
sudo apt install certbot python3-certbot-nginx -y
如果你使用 Nginx,执行:
sudo certbot --nginx -d example.com -d www.example.com
如果你使用 Apache,执行:
sudo apt install python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
查看证书:
sudo certbot certificates
测试自动续期:
sudo certbot renew --dry-run
手动续期:
sudo certbot renew
方案二:使用 Cloudflare Origin Certificate
进入 Cloudflare 后台:
SSL/TLS → Origin Server → Create Certificate
选择:
Let Cloudflare generate a private key and a CSR
填写域名:
example.com
*.example.com
Cloudflare 会生成:
- Origin Certificate
- Private Key
将证书保存到服务器。
创建目录:
sudo mkdir -p /etc/ssl/cloudflare
创建证书文件:
sudo nano /etc/ssl/cloudflare/origin.pem
粘贴 Cloudflare 提供的证书内容。
创建私钥文件:
sudo nano /etc/ssl/cloudflare/origin.key
粘贴 Cloudflare 提供的私钥内容。
修改权限:
sudo chmod 600 /etc/ssl/cloudflare/origin.key
sudo chmod 644 /etc/ssl/cloudflare/origin.pem
十、Nginx 配置示例
如果你使用 Nginx,可以参考以下配置。
创建站点配置文件:
sudo nano /etc/nginx/sites-available/example.com
写入:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/cloudflare/origin.pem;
ssl_certificate_key /etc/ssl/cloudflare/origin.key;
root /var/www/example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
创建网站目录:
sudo mkdir -p /var/www/example.com
创建测试页面:
echo 'Hello Cloudflare
' | sudo tee /var/www/example.com/index.html
启用站点:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
测试 Nginx 配置:
sudo nginx -t
重载 Nginx:
sudo systemctl reload nginx
如果报错,可以查看日志:
sudo journalctl -u nginx --no-pager
十一、Apache 配置示例
如果你使用 Apache,可以参考以下配置。
安装 Apache:
sudo apt update
sudo apt install apache2 -y
创建网站目录:
sudo mkdir -p /var/www/example.com
创建测试页面:
echo 'Hello Cloudflare
' | sudo tee /var/www/example.com/index.html
创建配置文件:
sudo nano /etc/apache2/sites-available/example.com.conf
写入:
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
SSLEngine on
SSLCertificateFile /etc/ssl/cloudflare/origin.pem
SSLCertificateKeyFile /etc/ssl/cloudflare/origin.key
ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined
启用 SSL 模块:
sudo a2enmod ssl
启用站点:
sudo a2ensite example.com.conf
禁用默认站点:
sudo a2dissite 000-default.conf
测试配置:
sudo apachectl configtest
重载 Apache:
sudo systemctl reload apache2
十二、开启强制 HTTPS
Cloudflare 后台进入:
SSL/TLS → Edge Certificates
开启:
Always Use HTTPS
建议同时开启:
Automatic HTTPS Rewrites
这可以将页面中的部分 HTTP 资源自动改写为 HTTPS,减少混合内容问题。
如果你的网站存在 HTTP 到 HTTPS 的跳转循环,通常是因为 SSL 模式设置成了 Flexible,而源站也配置了 HTTPS 跳转。此时应将 Cloudflare SSL 模式改为:
Full strict
十三、缓存配置入门
Cloudflare 最核心的能力之一是缓存静态资源。
进入:
Caching → Configuration
常见配置建议:
Browser Cache TTL: 4 hours 或 1 day
Caching Level: Standard
如果你的网站是 WordPress、Typecho、Halo、Hexo、Hugo、Vue、React、Next.js 等,可以根据资源类型设置缓存策略。
清理缓存
当你修改网站文件,但访问时仍然显示旧内容,可以清理 Cloudflare 缓存。
后台路径:
Caching → Configuration → Purge Cache
你可以选择:
Purge Everything
也可以只清理指定 URL:
https://example.com/style.css
使用 curl 检查缓存状态
执行:
curl -I https://example.com
你可能会看到:
cf-cache-status: HIT
常见状态包括:
| 状态 | 含义 |
|---|---|
| HIT | 命中缓存 |
| MISS | 未命中缓存 |
| BYPASS | 绕过缓存 |
| EXPIRED | 缓存过期 |
| DYNAMIC | 动态内容,不缓存 |
查看完整响应头:
curl -I -L https://example.com
十四、页面规则与缓存规则
Cloudflare 以前常用 Page Rules,现在新版本更推荐使用 Cache Rules、Redirect Rules、Configuration Rules。
常见规则一:强制跳转到 www 或非 www
如果希望 www.example.com 跳转到 example.com,可以使用 Redirect Rules。
规则示例:
If hostname equals www.example.com
Then URL redirect to https://example.com/$1
Status code: 301
如果使用 Nginx,也可以在源站配置。
Nginx 示例:
server {
listen 80;
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/ssl/cloudflare/origin.pem;
ssl_certificate_key /etc/ssl/cloudflare/origin.key;
return 301 https://example.com$request_uri;
}
常见规则二:静态资源长缓存
可以对以下路径设置较长缓存:
*.css
*.js
*.png
*.jpg
*.jpeg
*.gif
*.webp
*.svg
*.woff
*.woff2
建议:
Edge Cache TTL: 1 month
Browser Cache TTL: 1 month
但如果你的网站静态资源文件名不带版本号,例如一直叫 style.css,长缓存可能导致用户看到旧文件。推荐使用带 hash 的文件名,例如:
style.8f3a2c.css
app.7c9d1e.js
十五、安全设置入门
Cloudflare 安全功能很多,新手可以先做以下几项。
进入:
Security → Settings
建议设置:
Security Level: Medium
Challenge Passage: 30 minutes
Browser Integrity Check: On
开启 WAF 托管规则
进入:
Security → WAF → Managed rules
如果你的套餐支持,可以开启 Cloudflare Managed Ruleset。
如果你使用 WordPress,可以开启相关 WordPress 规则。
禁止恶意 IP 访问
进入:
Security → WAF → Custom rules
可以创建规则,例如阻止某个国家、某个 IP、某类请求路径。
示例:阻止某个 IP:
ip.src == 1.2.3.4
Action: Block
示例:阻止访问敏感路径:
http.request.uri.path contains "/.env"
Action: Block
示例:阻止扫描 WordPress 登录页:
http.request.uri.path == "/wp-login.php"
Action: Managed Challenge
十六、隐藏源站 IP
很多新手以为接入 Cloudflare 后就一定隐藏了源站 IP,其实不一定。
你需要确保:
- DNS 记录是橙色云朵,即
Proxied - 源站服务器防火墙只允许 Cloudflare IP 访问 80 和 443
- 不要在邮件记录、历史 DNS、GitHub、错误页面中泄露源站 IP
- 不要让源站 IP 直接返回与域名相同的网站内容
获取 Cloudflare IP 段
Cloudflare 官方 IP 列表地址:
https://www.cloudflare.com/ips/
你可以用命令获取:
curl -s https://www.cloudflare.com/ips-v4
curl -s https://www.cloudflare.com/ips-v6
使用 UFW 只允许 Cloudflare 访问网站端口
如果你使用 Ubuntu,并安装了 UFW,可以这样配置。
安装 UFW:
sudo apt install ufw -y
允许 SSH,避免把自己锁在外面:
sudo ufw allow 22/tcp
允许 Cloudflare IPv4 访问 80 和 443:
for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
sudo ufw allow from $ip to any port 80,443 proto tcp
done
允许 Cloudflare IPv6 访问 80 和 443:
for ip in $(curl -s https://www.cloudflare.com/ips-v6); do
sudo ufw allow from $ip to any port 80,443 proto tcp
done
开启防火墙:
sudo ufw enable
查看规则:
sudo ufw status numbered
注意:执行防火墙命令前,一定要确保 SSH 端口已经允许,否则可能无法远程登录服务器。
十七、恢复真实访客 IP
开启 Cloudflare 代理后,源站看到的访问 IP 默认可能是 Cloudflare 节点 IP,而不是用户真实 IP。Cloudflare 会通过请求头传递真实 IP,例如:
CF-Connecting-IP
如果你使用 Nginx,需要配置 real_ip。
编辑 Nginx 配置:
sudo nano /etc/nginx/conf.d/cloudflare-real-ip.conf
写入 Cloudflare IP 段,例如:
real_ip_header CF-Connecting-IP;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
测试配置:
sudo nginx -t
重载 Nginx:
sudo systemctl reload nginx
查看日志:
tail -f /var/log/nginx/access.log
十八、Cloudflare Tunnel 入门
Cloudflare Tunnel 是非常实用的功能。它可以让你在没有公网 IP、没有端口映射的情况下,把本地服务暴露到公网。
适合场景:
- 本地开发服务
- 家庭 NAS
- Home Assistant
- 内网 Web 面板
- 个人 Git 服务
- 临时演示网站
安装 cloudflared
Ubuntu / Debian 安装
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb
cloudflared --version
CentOS / Rocky Linux / AlmaLinux 安装
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-x86_64.rpm -o cloudflared.rpm
sudo rpm -i cloudflared.rpm
cloudflared --version
macOS 安装
brew install cloudflared
cloudflared --version
登录 Cloudflare
执行:
cloudflared tunnel login
浏览器会打开 Cloudflare 登录页面,选择你的域名授权。
创建 Tunnel
cloudflared tunnel create my-tunnel
查看 Tunnel 列表:
cloudflared tunnel list
创建配置文件
创建目录:
mkdir -p ~/.cloudflared
编辑配置:
nano ~/.cloudflared/config.yml
写入:
tunnel: my-tunnel
credentials-file: /home/你的用户名/.cloudflared/你的隧道ID.json
ingress:
- hostname: app.example.com
service: http://localhost:3000
- service: http_status:404
其中:
app.example.com是你想绑定的域名http://localhost:3000是本地服务地址credentials-file路径需要根据实际生成的 JSON 文件修改
绑定域名到 Tunnel
cloudflared tunnel route dns my-tunnel app.example.com
启动本地测试服务:
python3 -m http.server 3000
启动 Tunnel:
cloudflared tunnel run my-tunnel
然后访问:
https://app.example.com
如果能看到本地服务页面,就说明配置成功。
安装为系统服务
sudo cloudflared service install
启动服务:
sudo systemctl start cloudflared
设置开机自启:
sudo systemctl enable cloudflared
查看状态:
sudo systemctl status cloudflared
查看日志:
journalctl -u cloudflared -f
十九、使用 Cloudflare Workers 简单部署接口
Cloudflare Workers 是边缘计算服务,可以用来部署无服务器函数、API、反向代理、小工具等。
安装 Node.js 后,安装 Wrangler:
npm install -g wrangler
登录:
wrangler login
创建项目:
npm create cloudflare@latest hello-worker
进入项目:
cd hello-worker
本地开发:
npm run dev
部署:
npm run deploy
一个简单 Worker 示例:
export default {
async fetch(request, env, ctx) {
return new Response("Hello Cloudflare Workers!");
},
};
部署后你会得到一个类似这样的地址:
https://hello-worker.你的账号.workers.dev
也可以在 Cloudflare 后台绑定自定义域名。
二十、常见问题排查
1. 网站打不开,显示 DNS_PROBE_FINISHED_NXDOMAIN
可能原因:
- NS 还没生效
- DNS 记录没添加
- 域名写错
- 注册商处没有正确修改 nameserver
检查命令:
dig NS example.com
dig A example.com
2. 显示 521 Web Server Is Down
含义:Cloudflare 无法连接你的源站服务器。
排查:
curl -I http://你的源站IP
curl -I https://你的源站IP
sudo systemctl status nginx
sudo systemctl status apache2
sudo ufw status
常见原因:
- 源站服务器宕机
- Nginx 或 Apache 没启动
- 防火墙拦截了 Cloudflare
- 服务器安全组没有开放 80/443
3. 显示 522 Connection Timed Out
含义:Cloudflare 连接源站超时。
排查:
ping 你的源站IP
curl -I http://你的源站IP
traceroute 你的源站IP
常见原因:
- 服务器网络不通
- 源站响应太慢
- 防火墙规则错误
- 安全组没有放行
4. 显示 525 SSL Handshake Failed
含义:Cloudflare 和源站 HTTPS 握手失败。
排查:
openssl s_client -connect example.com:443 -servername example.com
常见原因:
- 源站证书配置错误
- Nginx SSL 配置错误
- SSL 模式选择不正确
- 证书和域名不匹配
建议使用:
SSL/TLS Mode: Full strict
并确保源站证书有效。
5. 显示重定向次数过多
通常是 HTTPS 跳转循环。
解决方法:
- 不要使用 Flexible 模式
- 改成 Full 或 Full strict
- 源站和 Cloudflare 不要互相重复错误跳转
推荐:
Cloudflare SSL/TLS: Full strict
源站 Nginx: HTTP 301 跳转 HTTPS
Cloudflare: Always Use HTTPS 开启
如果仍然循环,可以先关闭源站跳转或 Cloudflare Always Use HTTPS,逐项排查。
二十一、新手推荐配置清单
如果你刚开始使用 Cloudflare,可以按下面的配置来:
DNS
example.com → A → 服务器 IP → Proxied
www.example.com → CNAME → example.com → Proxied
SSL/TLS
SSL/TLS Mode: Full strict
Always Use HTTPS: On
Automatic HTTPS Rewrites: On
缓存
Caching Level: Standard
Browser Cache TTL: 4 hours 或 1 day
安全
Security Level: Medium
Browser Integrity Check: On
WAF Custom Rules: 按需配置
源站
Nginx 或 Apache 正常运行
源站安装有效 HTTPS 证书
防火墙只允许 Cloudflare 访问 80/443
SSH 单独限制访问
二十二、常用命令汇总
下面整理一份 Cloudflare 新手常用命令。
DNS 查询
dig NS example.com
dig A example.com
dig AAAA example.com
dig CNAME www.example.com
dig MX example.com
dig TXT example.com
dig +trace example.com
dig @1.1.1.1 example.com
dig @8.8.8.8 example.com
HTTP 检查
curl -I https://example.com
curl -I -L https://example.com
curl -v https://example.com
SSL 检查
openssl s_client -connect example.com:443 -servername example.com
Nginx
sudo nginx -t
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl status nginx
sudo journalctl -u nginx -f
Apache
sudo apachectl configtest
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo systemctl status apache2
sudo journalctl -u apache2 -f
Certbot
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot certificates
sudo certbot renew --dry-run
sudo certbot renew
Cloudflared
cloudflared --version
cloudflared tunnel login
cloudflared tunnel create my-tunnel
cloudflared tunnel list
cloudflared tunnel route dns my-tunnel app.example.com
cloudflared tunnel run my-tunnel
sudo cloudflared service install
sudo systemctl start cloudflared
sudo systemctl enable cloudflared
sudo systemctl status cloudflared
journalctl -u cloudflared -f
防火墙 UFW
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status numbered
只允许 Cloudflare 访问 80/443:
for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
sudo ufw allow from $ip to any port 80,443 proto tcp
done
for ip in $(curl -s https://www.cloudflare.com/ips-v6); do
sudo ufw allow from $ip to any port 80,443 proto tcp
done
二十三、总结
Cloudflare 对新手来说,最核心的使用路径其实并不复杂:
- 注册 Cloudflare 账号
- 添加域名
- 修改域名 NS 到 Cloudflare
- 配置 DNS 记录
- 开启代理 CDN
- 配置 SSL/TLS 为 Full strict
- 源站安装 HTTPS 证书
- 设置缓存和安全规则
- 按需使用 Cloudflare Tunnel 或 Workers
对于大多数网站来说,只要 DNS、SSL/TLS、缓存和安全规则配置正确,就能明显提升网站的访问稳定性、安全性和加载速度。
如果你是个人站长,建议先从免费套餐开始,熟悉 DNS、HTTPS、缓存和 Tunnel。等业务增长后,再根据需求考虑是否升级到 Pro、Business 或 Enterprise 套餐。Cloudflare 的强大之处不只是 CDN,而是它把网络、安全、边缘计算和访问控制整合到了一套平台里。掌握它之后,你会发现很多原本复杂的网络问题都可以用更简单的方式解决。