在昨天的文章中,我們已經順利在 Windows 本地端透過 Docker Compose 架構好了兩大核心引擎:n8n(自動化工作流引擎)與 Dify(AI 應用編排大腦),並成功串接 Google Gemini 模型完成對話驗證。
然而,本地端運行的服務預設只能在自己的電腦透過 localhost 存取。未來我們的 AI Agent 必須接收外部通訊軟體(如 LINE Bot、Telegram、Slack)的即時訊息,或是外部系統的 Webhook 回調。這時候,我們需要一條安全、快速的公網隧道(Tunnel)。
今天我們就來使用 ngrok,把本地端的 n8n 暴露到公網,並完成第一次外部 Webhook 觸發驗證!
https://)的網址,無法直接填寫 http://localhost:5678。在 Windows 系統中,建議直接使用 PowerShell 透過 winget 進行快速安裝:
winget install ngrok -s msstore
安裝完成後,可輸入以下指令確認是否安裝成功:
ngrok --version
若顯示版本號(例如 ngrok version 3.x.x),即代表安裝完成。
Step 2:註冊並取得驗證金鑰(Authtoken)
1.前往 ngrok 官網註冊頁面,可直接使用 Google 帳號快速登入。
2.登入後,點擊左側選單的 Your Authtoken。
3.複製頁面上的個人專屬 Token 字串。
4.回到 PowerShell,執行以下指令將 Token 綁定至本地設定檔:
ngrok config add-authtoken <你的_AUTHTOKEN>
驗證提示:終端機顯示 Authtoken saved to configuration file... 即表示金鑰寫入成功。
n8n 預設運行的連接埠為 5678。請在 PowerShell 中執行以下指令建立隧道:
ngrok http 5678
執行後,終端機會切換為 ngrok Session Status 儀表板:
Session Status online
Account wanglulu (Plan: Free)
Version 3.x.x
Region Asia-Pacific (ap)
Latency -
Web Interface [http://127.0.0.1:4040](http://127.0.0.1:4040)
Forwarding [https://xxxx-xx-xx-xx-xx.ngrok-free.app](https://xxxx-xx-xx-xx-xx.ngrok-free.app) -> http://localhost:5678
Forwarding 那一行顯示的 https://xxxx.ngrok-free.app 就是專屬的外部公網網址。
注意:終端機視窗必須保持開啟,關閉視窗隧道即會中斷。
公網網址建立好後,我們要在 n8n 建立一個簡單的測試工作流,用來驗證外部請求能否正常被本地端接收。
Step 1:新增 Webhook 節點
1.打開瀏覽器進入 n8n 介面(http://localhost:5678)。
2.點擊右上角 Add workflow 建立新流程。
3.點擊 Add first step,搜尋並選擇 Webhook 節點。
Step 2:設定 Webhook 參數
HTTP Method:選擇 POST(或 GET)。
Path:自訂路徑,例如 test-webhook。
Response Mode:選擇 On Received,在下方填入自訂回應(例如 {"status": "ok", "message": "Webhook received successfully!"})。
Step 3:取得公網 Webhook URL
n8n 畫面上會顯示兩組網址(Test URL 與 Production URL),預設通常是 http://localhost:5678/webhook-test/...。
將前端的 http://localhost:5678 替換成剛剛 ngrok 生成的 HTTPS 網址,組合成對外網址:
https://xxxx.ngrok-free.app/webhook-test/test-webhook
點擊節點下方的 Listen for test event,使 n8n 進入等待接收封包狀態。
打開另一個 PowerShell 視窗或使用 Postman / cURL 發送測試請求:
curl -X POST "[https://xxxx.ngrok-free.app/webhook-test/test-webhook](https://xxxx.ngrok-free.app/webhook-test/test-webhook)" `
-H "Content-Type: application/json" `
-d '{"user": "wanglulu", "text": "Hello Agent!"}'
```
驗收重點:
1.指令回傳:終端機成功收到 n8n 回傳的 {"status": "ok", ...}。
2.n8n 畫布:Webhook 節點變為綠色打勾,並在 Output 面板完整抓取到剛剛發送的 JSON 資料(user: wanglulu)。
3.ngrok 儀表板:ngrok 視窗出現 POST /webhook-test/test-webhook 200 OK 的日誌紀錄。
今日結語與明日預告
今天我們順利藉由 ngrok 跨越了本地防火牆與內網限制,成功實現外部網路與本地 n8n 引擎的雙向通訊!
至此,基礎建設已全數就緒:
運算節點:Docker Desktop (WSL 2)
大腦推論:Dify + Google Gemini
流程排程:n8n
外部通道:ngrok (HTTPS Tunnel)
明天 Day 04,我們將正式開始打造我們的第一個微服務核心:使用 Python FastAPI 建立專案骨架與健康檢查 API,敬請期待!