Skip to main content

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 đềlocalhostfree tunnel serviceCloudflare Tunnel
Domain thay đổiKhông có domainDomain random mỗi lần chạyCustom domain cố định
Config phải updateKhông share đượcPhải update config liên tụcSet once, dùng mãi
HTTPS supportCần setup manualCó sẵn nhưng domain thay đổiHTTPS + custom domain
External webhooksKhông nhận đượcMỗi lần test phải update URLURL 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ápMô tảUse case
Manual run (locally-managed)Quản lý tunnel qua CLI và config files localDevelopment, testing
Service modeTunnel chạy như system service, tự động startProduction, always-on
Zero Trust DashboardQuản lý tunnel qua web interfaceTeam collaboration
info

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.

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

1. Cài đặt cloudflared

note

Hướng dẫn installation cho macOS. Các platform khác tham khảo Cloudflare Installation Guide.

brew install cloudflared

2. Domain trỏ về Cloudflare

Prerequisites

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
Đ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

# Tạo tunnel với tên tùy chọn
cloudflared tunnel create my-dev-tunnel
Expected output
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:

~/.cloudflared/config.yml
tunnel: my-dev-tunnel
credentials-file: ~/.cloudflared/[UUID].json

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

  1. Vào Zero Trust Dashboard: https://one.dash.cloudflare.com
  2. AccessTunnelsmy-dev-tunnel
  3. Click "Add a public hostname"
  4. Thêm hostname và service:
    • Public hostname: app.yourdomain.com
    • Service: http://localhost:3000
  5. Save

Bước 5: Chạy tunnel

cloudflared tunnel run my-dev-tunnel
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=HAN
2024-06-19T10:30:00Z INF Registered tunnel connection

Quản lý Tunnel

Xem danh sách tunnels

cloudflared tunnel list
Example output
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

cloudflared tunnel --loglevel debug run my-dev-tunnel

Dừng tunnel

Nhấn Ctrl+C


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:

~/.cloudflared/config.yml
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
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

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:

~/.cloudflared/config.yml
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
Perfect for

Developers muốn stable development environment với custom domains thay vì depend vào random URLs từ free tunnel services.