กลับไปหน้าสูตร
#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 3000 | expose localhost:3000 |
ngrok http http://localhost:8080 | ระบุ URL โดยตรง |
ngrok http 3000 --host-header=rewrite | rewrite 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 22 | expose SSH |
ngrok tcp 3306 | expose database port |
ngrok tls 443 | expose TLS endpoint |
ngrok tcp 5432 | expose PostgreSQL |
Start Named Tunnels
| Command | คำอธิบาย |
|---|---|
ngrok start web | start tunnel ชื่อ web จาก config |
ngrok start web api | start หลาย tunnel |
ngrok start --all | start ทุก 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:4040 | dashboard ดู request/response |
| Inspect request body | debug payload webhook |
| Replay request | ยิง request เดิมซ้ำเพื่อทดสอบ |
| Compare responses | วิเคราะห์ bug ระหว่าง revision |
Security Best Practices
| เรื่อง | แนวทาง |
|---|---|
| เปิด endpoint สาธารณะ | ใช้ basic-auth หรือ verify signature |
| webhook | whitelist source + secret |
| token | เก็บใน .env / secret manager |
| tunnel สำคัญ | จำกัดช่วงเวลาการเปิดใช้งาน |
| logs | หลีกเลี่ยง data sensitive |
Webhook Testing Workflow
| Step | Action |
|---|---|
| 1 | รันแอป local (เช่น localhost:8787) |
| 2 | ngrok http 8787 |
| 3 | เอา HTTPS URL ไปใส่ใน provider webhook |
| 4 | เปิด :4040 เพื่อตรวจ payload |
| 5 | replay 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