Cloudflare Tunnel ใช้เว็บโดยไม่เปิด Port สาธารณะ

Cloudflare Tunnel ใช้เว็บโดยไม่เปิด Port สาธารณะ

มิถุนายน 2026 · อ่าน 7 นาที

Cloudflare Tunnel (ชื่อเดิม Argo Tunnel) ใช้ outbound connection จาก VPS ไปหา Cloudflare network แทนที่จะเปิด port 80/443 รับ inbound VPS ไม่ต้องมี public IP โดยตรง และ Cloudflare จะเป็น DDoS shield โดยอัตโนมัติ ใช้ฟรีใน Cloudflare Zero Trust Free plan

ติดตั้ง cloudflared

# ติดตั้ง cloudflared บน Ubuntu
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb \
  -o cloudflared.deb
sudo dpkg -i cloudflared.deb

# ตรวจ version
cloudflared --version

Login และสร้าง Tunnel

# Login กับ Cloudflare (จะเปิด browser)
cloudflared tunnel login

# สร้าง tunnel ใหม่
cloudflared tunnel create my-vps-tunnel

# ดู tunnel ที่สร้าง (จำ Tunnel ID)
cloudflared tunnel list

ตั้งค่า Tunnel Config

mkdir -p ~/.cloudflared
nano ~/.cloudflared/config.yml
tunnel: TUNNEL_ID_HERE
credentials-file: /root/.cloudflared/TUNNEL_ID.json

ingress:
  # Route domain ไปยัง local service
  - hostname: app.example.com
    service: http://localhost:3000
  - hostname: api.example.com
    service: http://localhost:8080
  # Catch-all (ต้องมีเสมอ)
  - service: http_status:404
# ตั้ง DNS record (ผ่าน Cloudflare API)
cloudflared tunnel route dns my-vps-tunnel app.example.com
cloudflared tunnel route dns my-vps-tunnel api.example.com

# ทดสอบ tunnel (รันแบบ foreground)
cloudflared tunnel run my-vps-tunnel

ติดตั้งเป็น System Service

# ติดตั้ง cloudflared เป็น service
sudo cloudflared --config /root/.cloudflared/config.yml service install

# เริ่ม service
sudo systemctl enable --now cloudflared

# ตรวจสถานะ
systemctl status cloudflared
Tip: หลังตั้ง Cloudflare Tunnel ให้ปิด inbound port 80 และ 443 ใน firewall ได้เลย ทำให้ VPS ไม่เปิดรับ connection ตรงจาก internet ทั้งหมด ยกเว้นผ่าน Cloudflare network

ตรวจสอบว่า Tunnel ทำงานถูกต้อง

# ดูสถานะ tunnel ใน Cloudflare dashboard
cloudflared tunnel info my-vps-tunnel

# ตรวจ service รัน
systemctl status cloudflared

# ดู log real-time
journalctl -u cloudflared -f

# ทดสอบเข้า URL จาก browser หรือ curl
curl -I https://app.example.com
Tip: ถ้า tunnel เป็น healthy แต่เว็บเข้าไม่ได้ ตรวจว่า local service รัน port ที่ระบุใน config หรือไม่: ss -tlnp | grep :3000

แก้ปัญหาที่พบบ่อย

Error: "failed to sufficiently increase receive buffer size"

cloudflared ต้องการ UDP buffer ขนาดใหญ่สำหรับ QUIC protocol:

sudo sysctl -w net.core.rmem_max=2500000
sudo sysctl -w net.core.wmem_max=2500000
echo "net.core.rmem_max=2500000" | sudo tee -a /etc/sysctl.conf

Tunnel connected แต่ DNS ยังไม่ชี้มา

DNS record ต้องเป็น CNAME ชี้ไป TUNNEL_ID.cfargotunnel.com ตรวจว่า route dns ทำงาน:

# ตรวจ DNS record
dig app.example.com CNAME

# ถ้าไม่มี ตั้งใหม่
cloudflared tunnel route dns my-vps-tunnel app.example.com

Service หยุดเองหลังรันไประยะหนึ่ง

อาจเป็นเพราะ credentials หมดอายุหรือ network timeout ตรวจ log:

journalctl -u cloudflared -n 100 | grep -i "error\|fatal\|disconnect"

# ลอง login ใหม่
cloudflared tunnel login

Website ช้ากว่าปกติหลังใช้ Tunnel

Cloudflare Tunnel เพิ่ม hop ผ่าน Cloudflare network อาจเพิ่ม latency 10-50ms ขึ้นกับ location ลอง enable Argo Smart Routing (เสียเงินเพิ่ม) หรือตรวจว่า Cloudflare node ที่ใกล้ที่สุดอยู่ที่ไหน


คำถามที่พบบ่อย

Cloudflare Tunnel ฟรีมี limit อะไรบ้าง?

Free plan รองรับ tunnel ไม่จำกัด connection ไม่จำกัด แต่ไม่มี SLA และ support Bandwidth ก็ไม่จำกัดเช่นกัน (Cloudflare ไม่คิดค่า bandwidth) ข้อจำกัดหลักคือ Zero Trust Access policies (จำกัด 50 user บน free plan)

ต่างจากการตั้ง Nginx Reverse Proxy ธรรมดาอย่างไร?

Nginx reverse proxy ยังเปิด port สาธารณะรอรับ connection Cloudflare Tunnel VPS เป็นฝ่าย connect ออกไปหา Cloudflare ทำให้ VPS ซ่อน IP จริงได้สมบูรณ์ และ DDoS จะถูก absorb ที่ Cloudflare ก่อนถึง VPS เลย

ใช้กับ SSH หรือ database ได้ไหม?

ได้ Cloudflare Tunnel รองรับ TCP tunneling ด้วย ทำให้ SSH หรือ database port เข้าถึงได้ผ่าน Zero Trust Access โดยไม่ต้องเปิด port สาธารณะ ต้องใช้ cloudflared บน client ด้วย