Cloudflare tunnel
Cloudflare Tunnel là gì?
Section titled “Cloudflare Tunnel là gì?”Cloudflare Tunnel là một dịch vụ cho phép bạn expose các ứng dụng chạy trên máy local ra internet một cách an toàn mà không cần mở port trên router hay firewall. Tunnel tạo ra một kết nối outbound từ máy tính của bạn đến Cloudflare edge network, sau đó Cloudflare sẽ proxy traffic từ internet về ứng dụng local của bạn.
Tại sao nên dùng Cloudflare Tunnel cho development?
Section titled “Tại sao nên dùng Cloudflare Tunnel cho development?”🎯 Vấn đề với localhost và free tunnel services
Section titled “🎯 Vấn đề với localhost và free tunnel services”| Vấn đề | localhost | free tunnel service | Cloudflare Tunnel |
|---|---|---|---|
| Domain thay đổi | Không có domain | Domain random mỗi lần chạy | Custom domain cố định |
| Config phải update | Không share được | Phải update config liên tục | Set once, dùng mãi |
| HTTPS support | Cần setup manual | Có sẵn nhưng domain thay đổi | HTTPS + custom domain |
| External webhooks | Không nhận được | Mỗi lần test phải update URL | URL không đổi |
💡 Core benefits của custom domain với Cloudflare Tunnel
Section titled “💡 Core benefits của custom domain với Cloudflare Tunnel”1. Domain cố định - Config một lần
Section titled “1. Domain cố định - Config một lần”# Thay vì mỗi lần chạy ngrok:# https://abc123.ngrok.io (session 1)# https://xyz789.ngrok.io (session 2)# → Phải update config mỗi lần!
# Với Cloudflare Tunnel:https://dev.yourdomain.com # LUÔN GIỐNG NHAU2. Webhook development dễ dàng
Section titled “2. Webhook development dễ dàng”# Shopify/GitHub/Stripe webhook URL:https://webhook.yourdomain.com/api/webhooks# ↑ Set một lần, không bao giờ thay đổiCách sử dụng Cloudflare Tunnel
Section titled “Cách sử dụng Cloudflare Tunnel”Có nhiều cách để setup và quản lý Cloudflare Tunnel:
| Phương pháp | Mô tả | Use case |
|---|---|---|
| Manual run (locally-managed) | Quản lý tunnel qua CLI và config files local | Development, testing |
| Service mode | Tunnel chạy như system service, tự động start | Production, always-on |
| Zero Trust Dashboard | Quản lý tunnel qua web interface | Team collaboration |
Trong docs này, chúng ta sẽ tập trung vào Manual run (locally-managed) vì đây là cách linh hoạt nhất cho development và cho phép bạn hiểu rõ cách hoạt động của tunnel.
:::note Platform Support Hướng dẫn này được thực hiện trên macOS. Các nền tảng khác (Linux, Windows) có workflow tương tự với một số điều chỉnh về installation và file paths.
Tham khảo Official Cloudflare Tunnel Documentation cho hướng dẫn chi tiết theo từng platform. :::
Prerequisites
Section titled “Prerequisites”1. Cài đặt cloudflared
Section titled “1. Cài đặt cloudflared”import Tabs from ‘@theme/Tabs’; import TabItem from ‘@theme/TabItem’;
brew install cloudflared# Download from GitHub releaseswget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-amd64.tgztar -xzf cloudflared-darwin-amd64.tgzsudo mv cloudflared /usr/local/bin/2. Domain trỏ về Cloudflare
Section titled “2. Domain trỏ về Cloudflare”:::warning Prerequisites Bạn cần có domain đã được add vào Cloudflare và nameservers đã pointing về Cloudflare. :::
Setup Manual Tunnel (Locally-managed)
Section titled “Setup Manual Tunnel (Locally-managed)”Bước 1: Authenticate với Cloudflare
Section titled “Bước 1: Authenticate với Cloudflare”cloudflared tunnel login:::note Điều gì sẽ xảy ra? Lệnh này sẽ:
- Mở browser để bạn login vào Cloudflare account
- Chọn domain bạn muốn sử dụng
- Download certificate vào
~/.cloudflared/cert.pem(macOS/Linux) hoặc%USERPROFILE%\.cloudflared\cert.pem(Windows) :::
Bước 2: Tạo tunnel
Section titled “Bước 2: Tạo tunnel”# Tạo tunnel với tên tùy chọncloudflared tunnel create my-dev-tunnelTunnel credentials written to ~/.cloudflared/[UUID].json (macOS/Linux)Created tunnel my-dev-tunnel with id [UUID]Bước 3: Tạo file config
Section titled “Bước 3: Tạo file config”Tạo file ~/.cloudflared/config.yml:
tunnel: my-dev-tunnelcredentials-file: ~/.cloudflared/[UUID].json:::info Config từ Dashboard
File config.yml chỉ chứa basic tunnel settings. Ingress rules (hostname → service mapping) sẽ được config từ Zero Trust Dashboard thay vì file local.
:::
Bước 4: Config ingress từ Zero Trust Dashboard
Section titled “Bước 4: Config ingress từ Zero Trust Dashboard”- Vào Zero Trust Dashboard: https://one.dash.cloudflare.com
- Access → Tunnels → my-dev-tunnel
- Click “Add a public hostname”
- Thêm hostname và service:
- Public hostname:
app.yourdomain.com - Service:
http://localhost:3000
- Public hostname:
- Save
Bước 5: Chạy tunnel
Section titled “Bước 5: Chạy tunnel”cloudflared tunnel run my-dev-tunnel:::tip Successful connection Khi thành công, bạn sẽ thấy log tương tự:
2024-06-19T10:30:00Z INF Connection established location=HAN2024-06-19T10:30:00Z INF Registered tunnel connection:::
Quản lý Tunnel
Section titled “Quản lý Tunnel”Xem danh sách tunnels
Section titled “Xem danh sách tunnels”cloudflared tunnel listID NAME CREATEDabc123-def456-ghi789 my-dev-tunnel 2024-06-19T10:00:00ZTest cấu hình ingress
Section titled “Test cấu hình ingress”# Test hostname sẽ route về service nàocloudflared tunnel ingress url https://app.yourdomain.com
# Validate cấu hình ingresscloudflared tunnel ingress validateDebug và logs
Section titled “Debug và logs”cloudflared tunnel --loglevel debug run my-dev-tunnelnohup cloudflared tunnel run my-dev-tunnel > tunnel.log 2>&1 &Dừng tunnel
Section titled “Dừng tunnel”Nhấn Ctrl+C
# Tìm processps aux | grep cloudflared
# Kill specific processkill [PID]
# Hoặc kill tất cả cloudflared processespkill cloudflaredVí dụ thực tế
Section titled “Ví dụ thực tế”Ví dụ 1: Thay thế ngrok cho webhook development
Section titled “Ví dụ 1: Thay thế ngrok cho webhook development”Vấn đề với ngrok free:
# Mỗi lần restart ngrok:ngrok http 3000# → https://abc123.ngrok.io (khác mỗi lần)# → Phải update webhook URL ở Shopify/GitHub/Stripe...Giải pháp với Cloudflare Tunnel:
tunnel: my-webhookcredentials-file: ~/.cloudflared/abc123-def456-ghi789.jsonConfig từ Zero Trust Dashboard:
- Public hostname:
webhook.yourdomain.com - Service:
http://localhost:3000
# Chạy tunnelcloudflared tunnel run my-webhook
# Webhook URL LUÔN GIỐNG NHAU:# https://webhook.yourdomain.com:::success Lợi ích
- ✅ URL không đổi - Set webhook một lần
- ✅ Không cần update config mỗi khi dev restart :::
Ví dụ 2: Custom domain cho development app
Section titled “Ví dụ 2: Custom domain cho development app”Vấn đề với localhost:
# Không share được:http://localhost:3000 # Chỉ máy local access được
# Không test mobile được:# Cần config network phức tạpGiải pháp với Cloudflare Tunnel:
tunnel: dev-appcredentials-file: ~/.cloudflared/abc123-def456-ghi789.jsonConfig từ Zero Trust Dashboard:
- Public hostname:
dev.yourdomain.com - Service:
http://localhost:3000
# Start appnpm run dev # localhost:3000
# Start tunnelcloudflared tunnel run dev-app
# Access từ bất kỳ đâu:# https://dev.yourdomain.com:::
Kết luận
Section titled “Kết luận”Manual run (locally-managed) tunnel mang lại stable custom domains thay thế hoàn hảo cho localhost và ngrok free:
- ✅ Custom domain cố định - Không thay đổi mỗi session
- ✅ Set once, use forever - Config một lần, dùng mãi mãi
- ✅ Webhook development - External services không cần update URLs
:::info Perfect for Developers muốn stable development environment với custom domains thay vì depend vào random URLs từ free tunnel services. :::