Cloudflare Tunnel
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?
🎯 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
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 NHAU
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 đổi
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.
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
1. Cài đặt cloudflared
Hướng dẫn installation cho macOS. Các platform khác tham khảo Cloudflare Installation Guide.
- macOS (Recommended)
- Manual Download
brew install cloudflared
# Download from GitHub releases
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-amd64.tgz
tar -xzf cloudflared-darwin-amd64.tgz
sudo mv cloudflared /usr/local/bin/
2. Domain trỏ về Cloudflare
Bạn cần có domain đã được add vào Cloudflare và nameservers đã pointing về Cloudflare.
Setup Manual Tunnel (Locally-managed)
Bước 1: Authenticate với Cloudflare
cloudflared tunnel login
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
# Tạo tunnel với tên tùy chọn
cloudflared tunnel create my-dev-tunnel
Tunnel credentials written to ~/.cloudflared/[UUID].json (macOS/Linux)
Created tunnel my-dev-tunnel with id [UUID]
Bước 3: Tạo file config
Tạo file ~/.cloudflared/config.yml
:
tunnel: my-dev-tunnel
credentials-file: ~/.cloudflared/[UUID].json
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
- 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
cloudflared tunnel run my-dev-tunnel
Khi thành công, bạn sẽ thấy log tương tự:
2024-06-19T10:30:00Z INF Connection established location=HAN
2024-06-19T10:30:00Z INF Registered tunnel connection
Quản lý Tunnel
Xem danh sách tunnels
cloudflared tunnel list
ID NAME CREATED
abc123-def456-ghi789 my-dev-tunnel 2024-06-19T10:00:00Z
Test cấu hình ingress
# Test hostname sẽ route về service nào
cloudflared tunnel ingress url https://app.yourdomain.com
# Validate cấu hình ingress
cloudflared tunnel ingress validate
Debug và logs
- Debug mode
- Background với log
cloudflared tunnel --loglevel debug run my-dev-tunnel
nohup cloudflared tunnel run my-dev-tunnel > tunnel.log 2>&1 &
Dừng tunnel
- Foreground process
- Background process
Nhấn Ctrl+C
# Tìm process
ps aux | grep cloudflared
# Kill specific process
kill [PID]
# Hoặc kill tất cả cloudflared processes
pkill cloudflared
Ví dụ thực tế
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-webhook
credentials-file: ~/.cloudflared/abc123-def456-ghi789.json
Config từ Zero Trust Dashboard:
- Public hostname:
webhook.yourdomain.com
- Service:
http://localhost:3000
# Chạy tunnel
cloudflared tunnel run my-webhook
# Webhook URL LUÔN GIỐNG NHAU:
# https://webhook.yourdomain.com
- ✅ 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
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ạp
Giải pháp với Cloudflare Tunnel:
tunnel: dev-app
credentials-file: ~/.cloudflared/abc123-def456-ghi789.json
Config từ Zero Trust Dashboard:
- Public hostname:
dev.yourdomain.com
- Service:
http://localhost:3000
# Start app
npm run dev # localhost:3000
# Start tunnel
cloudflared tunnel run dev-app
# Access từ bất kỳ đâu:
# https://dev.yourdomain.com
:::
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
Developers muốn stable development environment với custom domains thay vì depend vào random URLs từ free tunnel services.