blogbuituantuBùi Tuấn Tú

Config Cloudflare Tunnel

17 tháng 11, 2024

0. Kích hoạt Cloudflare Tunnel (Zero Trust)

Kích hoạt Cloudflare Tunnel trong Zero Trust

1. Tải xuống công cụ

Danh sách bản tải Cloudflared Chọn phiên bản Cloudflared cho Windows

2. Thiết lập biến môi trường

  1. Đổi tên file cloudflared-windows-amd64.exe thành cloudflared.exe.
  2. Di chuyển file vào C:\Program Files\Cloudflare\cloudflared\cloudflared.exe.
  3. Thêm đường dẫn vào biến môi trường PATH của hệ thống:
powershell
[Environment]::SetEnvironmentVariable(
  "PATH",
  $env:PATH + ";C:\Program Files\Cloudflare\cloudflared",
  [EnvironmentVariableTarget]::Machine
)

3. Kiểm tra cài đặt

powershell
cloudflared --version

Ví dụ kết quả thành công:

powershell
PS C:\WINDOWS\system32> cloudflared --version
cloudflared version 2025.10.1 (built 2025-10-30T14:56 UTC)

4. Đăng nhập và chọn domain

powershell
cloudflared login
Chọn domain trong bước đăng nhập File cert.pem và credential được sinh ra

Tạo thêm file config.yml với nội dung mẫu:

yaml
tunnel: <tunnel-id>
credentials-file: C:\Users\buituantu\.cloudflared\<tunnel-id>.json
 
ingress:
  # Backend API (.NET)
  - hostname: api.tinhnguyenvien.io.vn
    service: http://localhost:5000
 
  # Frontend (Angular)
  - hostname: tinhnguyenvien.io.vn
    service: http://localhost:4200
 
  # Mặc định:
  - service: http_status:404

Ghi chú: tunnel-id sẽ được tạo ở bước tiếp theo và cần điền vào cả tunnel lẫn credentials-file.

5. Tạo tunnel

powershell
cloudflared tunnel create <TEN_TUNNEL>

Ví dụ:

powershell
cloudflared tunnel create buituantu-tunnel

Kết quả mẫu:

powershell
PS C:\WINDOWS\system32> cloudflared tunnel create buituantu-tunnel
Tunnel credentials written to C:\Users\buituantu\.cloudflared\0bde6ece-b2a0-4f38-be68-6901ed2cfbde.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 buituantu-tunnel with id 0bde6ece-b2a0-4f38-be68-6901ed2cfbde

Sau khi nhận tunnel-id, cập nhật lại file config.yml.

6. Gắn domain vào tunnel

powershell
cloudflared tunnel route dns <TEN_TUNNEL> <DOMAIN>

Nếu trong config.yml có hai domain api.tinhnguyenvien.io.vntinhnguyenvien.io.vn, chạy hai lệnh:

powershell
cloudflared tunnel route dns tinhnguyenvien-tunnel tinhnguyenvien.io.vn
cloudflared tunnel route dns tinhnguyenvien-tunnel api.tinhnguyenvien.io.vn

Cloudflare sẽ trả về thông báo Added CNAME ... khi thành công.

7. Khởi chạy code và tunnel

powershell
cloudflared tunnel run tinhnguyenvien-tunnel

Lưu ý: Khi triển khai, hãy cập nhật cấu hình frontend để gọi đúng endpoint backend trên domain public. Nếu frontend vẫn trỏ về localhost, người dùng bên ngoài sẽ không gọi được API.

8. Các lệnh hữu ích khác

Chỉ với chuỗi bước trên, bạn đã có thể dựng Cloudflare Tunnel, ánh xạ domain và chia sẻ môi trường nội bộ ra internet một cách an toàn.