The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

如何在 Windows 安裝和設定 Cloudflare Tunnel 服務

Cloudflare Tunnel 是 Cloudflare 提供的一種服務,允許你將本地服務安全地暴露給互聯網,而無需開放防火牆或設定路由器。我最近因為重新整理了一台測試機,整理了一些筆記,本篇文章我打算記錄如何在一台乾淨的 Windows Server 環境中安裝和設定 Cloudflare Tunnel 服務的過程。

Image

安裝 Cloudflare Tunnel

你必須先下載正確的 cloudflared 命令列工具。

透過 winget 的安裝命令如下:

winget install --id Cloudflare.cloudflared

請勿使用 chocolatey 安裝!

你也可以從 GitHub Release 下載最新版本回來安裝。

設定 Cloudflare Tunnel

請依照以下步驟設定即可:

  1. 登入 Cloudflare 帳號並產生 ~\.cloudflared\cert.pem 通道憑證 (Argo Tunnel Token)

    cloudflared tunnel login
    

    選擇 your-domain.com 域名

  2. 列出現有 Cloudflare 帳號下的通道

    建立 my-tunnel 通道 (建立過就不用再次建立)

    cloudflared tunnel create my-tunnel
    

    回傳內容:

    Tunnel credentials written to C:\Users\Administrator\.cloudflared\8f721c95-xxxx-xxxx-xxxx-90f59448c88e.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.
    
    Created tunnel my-tunnel with id 8f721c95-xxxx-xxxx-xxxx-90f59448c88e
    

    💡 這裡 8f721c95-xxxx-xxxx-xxxx-90f59448c88e 就是通道編號 (Tunnel-UUID)。

    列出通道資訊

    cloudflared tunnel list
    

    查詢通道資訊

    cloudflared tunnel info my-tunnel
    

    注意: 這裡 cloudflared tunnel list 會顯示你的通道編號 (Tunnel-UUID),這是跟著 Cloudflare 帳號 (Account) 走的,而不是跟著網域 (Domain) 走的。

  3. 用取得到的 Tunnel Token (Tunnel credentials) 建立 CNAME 域名

    cloudflared tunnel route dns 'my-tunnel' 'testing-5678.your-domain.com'
    
  4. 手動建立 ~/.cloudflared/config.yml 設定檔案

    tunnel: 8f721c95-xxxx-xxxx-xxxx-90f59448c88e
    
    ingress:
    
      - hostname: testing-5678.your-domain.com
        service: http://localhost
    
      - service: http_status:404
    

    注意: 這裡 8f721c95-xxxx-xxxx-xxxx-90f59448c88e 就是你的 Tunnel UUID,此值可從 cloudflared tunnel list 命令中獲取。

  5. 測試並執行

    確認設定無誤後,即可啟動 Tunnel:

    cloudflared tunnel run my-tunnel
    

這樣就完成了 Cloudflare Tunnel 的設定,現在你可以透過 testing-5678.your-domain.com 這個域名來訪問你本地的服務了。

相關連結

留言評論