กลับไปหน้าสูตร
#ngrok#command#tunnel#cheatsheet

ngrok Command Cheatsheet

รวมคำสั่ง ngrok แบบจัดเต็ม: setup, http/tcp/tls tunnels, config file, security, inspect traffic, และ webhook workflow

11 มีนาคม 2569อ่านประมาณ 2 นาที

Install & Setup

Commandคำอธิบาย
ngrok versionตรวจเวอร์ชัน
ngrok config add-authtoken <TOKEN>เพิ่ม auth token
ngrok config checkตรวจ config ว่าถูกต้อง
ngrok config editเปิดแก้ไฟล์ config
ngrok helpดูคำสั่งทั้งหมด

HTTP Tunnels

Commandคำอธิบาย
ngrok http 3000expose localhost:3000
ngrok http http://localhost:8080ระบุ URL โดยตรง
ngrok http 3000 --host-header=rewriterewrite Host header
ngrok http 3000 --domain=<your-domain>ใช้ custom domain
ngrok http 3000 --basic-auth='user:pass'basic auth หน้า endpoint
ngrok http 3000 --request-header-add='x-env:dev'แทรก header

TCP / TLS Tunnels

Commandคำอธิบาย
ngrok tcp 22expose SSH
ngrok tcp 3306expose database port
ngrok tls 443expose TLS endpoint
ngrok tcp 5432expose PostgreSQL

Start Named Tunnels

Commandคำอธิบาย
ngrok start webstart tunnel ชื่อ web จาก config
ngrok start web apistart หลาย tunnel
ngrok start --allstart ทุก tunnel ใน config

Config File Example

version: "2"
authtoken: <YOUR_TOKEN>

region: ap

web_addr: 127.0.0.1:4040

log_level: info

agent:
  inspect_db_size: 0

tunnels:
  web:
    proto: http
    addr: 3000
    schemes:
      - https
    basic_auth:
      - "dev:devpass"
  webhook:
    proto: http
    addr: 8787
    request_header_add:
      x-source: ngrok
  ssh:
    proto: tcp
    addr: 22

Inspect & Replay

Toolใช้ทำอะไร
http://127.0.0.1:4040dashboard ดู request/response
Inspect request bodydebug payload webhook
Replay requestยิง request เดิมซ้ำเพื่อทดสอบ
Compare responsesวิเคราะห์ bug ระหว่าง revision

Security Best Practices

เรื่องแนวทาง
เปิด endpoint สาธารณะใช้ basic-auth หรือ verify signature
webhookwhitelist source + secret
tokenเก็บใน .env / secret manager
tunnel สำคัญจำกัดช่วงเวลาการเปิดใช้งาน
logsหลีกเลี่ยง data sensitive

Webhook Testing Workflow

StepAction
1รันแอป local (เช่น localhost:8787)
2ngrok http 8787
3เอา HTTPS URL ไปใส่ใน provider webhook
4เปิด :4040 เพื่อตรวจ payload
5replay request หลังแก้โค้ด

Common Problems

ปัญหาวิธีเช็ก
401/403ตรวจ authtoken / basic-auth
provider ส่ง webhook ไม่เข้าตรวจ URL ล่าสุดจาก ngrok
host header mismatchใช้ --host-header=rewrite
local app ไม่ตอบสนองเช็กแอป local ว่ารันอยู่และพอร์ตตรง
tunnel หลุดเช็ก network / แพ็กเกจ / plan limitations

Useful Commands (One-liners)

# เปิด tunnel dev web
ngrok http 3000 --host-header=rewrite

# เปิด webhook พร้อม basic auth
ngrok http 8787 --basic-auth='dev:devpass'

# เปิดจาก config ทั้งหมด
ngrok start --all