1. 注册 ngrok 账号
- 访问 dashboard.ngrok.com/signup
- 使用 GitHub / Google 登录,或者创建一个新账号
- 进入 "Your Authtoken" 页面:dashboard.ngrok.com/get-started…
2. 在终端中配置 authtoken
复制 ngrok 提供的 authtoken,然后在终端运行:
ngrok config add-authtoken YOUR_NGROK_AUTH_TOKEN
把 YOUR_NGROK_AUTH_TOKEN 替换为你的 authtoken。
3. 重新运行 ngrok
ngrok http 8090
成功后,你会看到类似:
Forwarding https://abcd1234.ngrok.io -> http://localhost:8090
现在,你可以在浏览器或局域网设备访问 https://abcd1234.ngrok.io。
你的 ngrok 账号是 免费版,所以 不能使用 --subdomain 参数。要固定 ngrok 生成的 Forwarding 地址,你可以选择以下方法:
✅ 方法 1:使用 localtunnel(免费,支持固定子域名)
如果你只想免费固定子域名,可以用 localtunnel 代替 ngrok:
npx localtunnel --port 8090 --subdomain haixiao911
- 这样你就可以通过
https://haixiao911.loca.lt访问http://localhost:8090。 - ⚠️
localtunnel可能会有短时间掉线问题,但可以重新运行获取相同的地址。
✅ 方法 2:使用 cloudflared(免费,性能更稳定)
如果 localtunnel 不稳定,你可以使用 Cloudflare 的 cloudflared(支持长期运行):
-
安装
cloudflared:brew install cloudflared # macOS sudo apt install cloudflared # Ubuntu/Debian -
运行
cloudflared:cloudflared tunnel --url http://localhost:8090Cloudflare 会返回一个固定的
ForwardingURL,可以长期使用。
✅ 方法 3:购买 ngrok 付费计划
如果你坚持使用 ngrok 并想固定 Forwarding 地址,你需要升级到 Pro 付费计划:
-
升级到
Pro计划($8/月):
🔗 升级地址 -
然后使用
--hostname指定你的域名:ngrok http --hostname=yourdomain.com 8090
💡 结论
| 方案 | 费用 | 优势 | 缺点 |
|---|---|---|---|
localtunnel | 免费 | 简单、支持固定子域名 | 不太稳定,可能掉线 |
cloudflared | 免费 | 稳定、长期可用 | 需要 Cloudflare |
ngrok Pro | 付费($8/月) | 正式、商业级 | 需要付费 |
如果只是临时用,推荐 localtunnel 或 cloudflared 🚀