Start multiple Port

จากบทความก่อนหน้า 👉Ngrok เราได้รู้แล้วว่า ngrok มีความสามารถอะไรเนอะ ถ้าไม่รู้ก็กลับไปอ่านได้เลย

ที่นี้เวลาที่เราทำงานจริงๆ ถ้าเราเป็น full stack เป็นไปไม่ได้เลยเนอะที่เราจะ start web server แค่ port เดียวส่วนใหญ่แล้ว Web server 1 port และ API server 1 port เช่นเราทำ Line Chatbot web ที่จะให้ Line เปิดก็ 1 public URL แล้วกรณีมี webhook ก็ต้องใช้อีก 1 public URL

ถ้าเรา start ngrok ผ่าน basic command จะเห็นว่ามันให้เรา input port ที่อยากจะ start ไปได้แค่ port เดียว

มันก็ไม่ตอบโจทย์เราสิ งั้นทำยังไงละ บทความนี้มีคำตอบ

หลังจากติดตั้งและใช้งาน ngrok ได้แล้วจะมี file ngrok.yml ด้วย โดยปกติจะอยู่ที่

C:\Users\{username}\.ngrok2

แต่ละ OS ก็จะอยู่ตามนี้

  • Linux: "~/.config/ngrok/ngrok.yml"

  • MacOS (Darwin): "~/Library/Application Support/ngrok/ngrok.yml"

  • Windows: "C:\Users\{username}\.ngrok\ngrok.yml"

พอเปิด file ขึ้นมาจะเห็นว่ามี line เดียวก็คือที่เรา add authtoken ไวตอนแรกเลย

ให้เราเพิ่ม config เข้าไปแบบนี้

authtoken: {your authtoken}
tunnels:
  testweb:
    addr: {your port of web server}
    proto: http
    bind_tls: true
  testapi:
    addr: {your port api server}
    proto: http
    bind_tls: false

ใน config นี้บอกว่าเรามี tunnels อยู่ 2 ตัวชื่อ testweb และ testapi โดยแต่ละ tunnel นั้น

  • addr คือใช้ port อะไร

  • proto คือ protocol ที่ start คือ http นะ

  • bind_tls คือต้อง start https ให้ด้วยไหม

แล้วเวลา start ngrok เปลี่ยนเป็นคำสั่งแบบนี้แทน

ngrok start --all

หรืออยากระบุเป็น tunnels ืname ก็ทำได้เหมือนกันแบบนี้

ngrok start testweb testapi

ผลลัพธ์ที่ได้ก็จะเป็นตามนี้

Reference

Last updated

Was this helpful?