How I Connected a Netlify Function to My Home Server Using a Dynamic Cloudflare Tunnel
说明与用途
为 Cloudflare 隧道创建永久 DNS 记录,作为 systemd 服务运行,并使用 Netlify Function 的环境变量访问家庭 API,消除了隧道重启时的 404 错误。
问题
Netlify Functions needed to reach a home‑server API, but Cloudflare Tunnel URLs changed on restart, causing 404s and manual secret updates.
结果
A permanent DNS hostname for the Cloudflare tunnel, automated service startup, and environment‑variable‑based API calls eliminate manual updates and 404 errors, ensuring reliable communication between Netlify front‑end and home automation stack.
工作流
- Create a named Cloudflare tunnel and route a permanent DNS name to it.
- Configure cloudflared with a config.yml pointing to local service and hostname.
- Enable and start cloudflared as a systemd service.
- Store the stable hostname in a Netlify environment variable and fetch it in the function.
- Set up a health‑check Netlify Function that polls Cloudflare metrics and restarts the service via SSH if inactive.
证据
When I moved the front‑end to Netlify, my Netlify Functions needed to hit that internal API. A Cloudflare Tunnel (formerly Argo Tunnel) solved the NAT issue, but every time the tunnel restarted its public URL changed.
Create a named tunnel (only once) cloudflared tunnel create home‑api
cloudflared tunnel route dns home‑api home.api.mybiz.com
/etc/cloudflared/config.yml url: http://localhost:3000 hostname: home.api.mybiz.com
sudo systemctl enable cloudflared@home‑api sudo systemctl start cloudflared@home‑api
exports.handler = async () => { const base = process.env.HOME_API_URL || "https://home.api.mybiz.com"; const res = await fetch(${base}/status); const data = await res.json(); return { statusCode: 200, body: JSON.stringify(data) }; };
curl -s https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/tunnels | jq '.result[] | select(.name=="home‑api") | .status'
- 记录日期: 2026-10-18T13:41:16.528Z