Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Connecting a Netlify Function to a Home Server via a Dynamic Cloudflare Tunnel

说明与用途

Netlify 函数动态获取 Cloudflare 隧道 URL 并代理请求到本地服务器,消除了静态主机名的需求。

问题

How to call a home server from a Netlify function when the Cloudflare tunnel address changes on each restart?

结果

The solution allows a Netlify function to reliably call a home server behind a dynamic Cloudflare tunnel by querying the tunnel URL at runtime, eliminating the need for static hostnames or port forwarding.

工作流

  • Install cloudflared on the Pi and configure it to run as a background service without a static hostname.
  • Start the tunnel with cloudflared tunnel run myhome-tunnel & and note that Cloudflare assigns a random public URL.
  • Create a Cloudflare API token with Zone:Read and Tunnel:Read permissions and store it in Netlify’s environment variables (CLOUDFLARE_TOKEN).
  • Write a Node script to call the Cloudflare API endpoint https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/cfd_tunnel and extract the tunnel_url for the tunnel named "myhome-tunnel".
  • Implement a Netlify function (netlify/functions/ping-home.js) that: 1) fetches the tunnel URL from the Cloudflare API, 2) appends the desired path (e.g., /status), 3) forwards the request to the home server and returns the response to the client.
  • Store the Cloudflare account ID in Netlify’s environment variables (CLOUDFLARE_ACCOUNT_ID).

证据

I run a small automation server at home that powers a few cron jobs and a private API for my own website.

来源

Cloudflare Tunnel (formerly Argo Tunnel) creates a secure outbound connection from my Pi to Cloudflare’s edge network.

来源

I created a token with the Zone:Read and Tunnel:Read permissions scoped to my account.

来源

The function runs on Netlify’s edge, so the outbound request must be over HTTPS. Cloudflare’s tunnel URL is always HTTPS, which satisfies that.

来源

  • 记录日期: 2026-10-27T13:26:12.633Z