Cloudflare Tunnel全流程配置实现NAS应用直接用域名访问
✔ 适用于:Debian / Ubuntu / CentOS
✔ 不需要公网 IP
✔ 不需要开放路由器端口
✔ 自动 HTTPS
✔ 自动证书
✔ 自动反向代理你的 Flask / Nginx / 静态站点
Cloudflare Tunnel 全流程配置
🚀 第一步:安装 Cloudflare Tunnel(cloudflared)
Debian/Ubuntu 一键安装:
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb检查版本:
cloudflared --version🚀 第二步:登录 Cloudflare 账号
cloudflared tunnel login执行后会弹出一个 URL
复制到浏览器 → Cloudflare → 选择你要使用的域名(如 m.zenjong.site)
绑定成功后,会生成一个证书文件:
/root/.cloudflared/cert.pem🚀 第三步:创建 Tunnel
cloudflared tunnel create m_tunnel会生成:
Tunnel ID:xxxxxx-xxxx-xxxx-xxxx .jsonxxx.json 改成m_tunnel.json
配置文件目录:
/root/.cloudflared/🚀 第四步:编写 Tunnel 配置文件(核心步骤)
创建配置文件:
nano /root/.cloudflared/config.yml写入以下内容(完全不用改)👇
⭐ 示例:反代你的 Flask 服务(本地 5000 端口)
tunnel: m_tunnel
credentials-file: /root/.cloudflared/m_tunnel.json
ingress:
- hostname: m.zenjong.site
service: http://localhost:4533
- service: http_status:404
🚀 第五步:创建 Cloudflare DNS 记录(自动完成)
执行:
cloudflared tunnel route dns m_tunnel m.zenjong.siteCloudflare 会自动创建一个 CNAME:
Added CNAME m.zenjong.site which will route to this tunnel tunnelID=9233dd4c-0878-4b2c-ad9c-147a0b6312b3cloudflared tunnel run au_tunnel
🌟 不需要你手动在 Cloudflare 后台添加 DNS!
🚀 第六步:创建 Systemd 自启动服务(强烈推荐)
cloudflared service install启动服务:
systemctl start cloudflared
systemctl enable cloudflared查看日志:
journalctl -fu cloudflared看到:
Connection established说明成功!
🚀 第七步:访问你的公网 HTTPS 域名(完成)
直接访问:
https://m.zenjong.site```
你会看到:
* 自动 HTTPS
* 自动证书
* 自动 CDN 加速(如需)
* 不需要开放 5000 端口
* 不需要 NAT
* 不需要公网 IP
---
# ⭐ 完整目录结构(自动生成):
/root/.cloudflared
├── cert.pem
├── d_tunnel.json
└── config.yml
---
# 🧪 如何测试?
curl -I https://m.zenjong.site
返回 200/302 即正常。
---
# 🛟 可选:反向代理多个服务
例如:
* API → api.rongyi.site → 127.0.0.1:5001
* 前端 → web.rongyi.site → 127.0.0.1:80
* 文件服务 → file.rongyi.site → 127.0.0.1:9000
配置:
ingress:
- hostname: dms.rongyi.site
service: http://localhost:5000 - hostname: api.rongyi.site
service: http://localhost:5001 - hostname: web.rongyi.site
service: http://localhost:80 - service: http_status:404
就能反代多个域名。
