Skip to content

内网穿透

本地 HTTP/WS 服务暴露外网完整教程

localtunnel

1. 确保 Node.js 已安装

先确认你机器上有 Node.js(带 npm):

sh
node -v
npm -v

如果没有,就去 Node.js 官网 安装 LTS 版本。


2. 安装 localtunnel

在你的项目目录下执行:

sh
npm install -g localtunnel

这会全局安装 lt 命令。


3. 启动本地服务

假设你的项目里已经有:

  • 一个 HTTP 服务(本地 7777端口)
  • 一个 WebSocket 服务(本地 18023端口)

你可以直接跑它们(比如 npm run dev,具体看你项目)。


4. 暴露 HTTP 服务

新开一个终端窗口,执行:

sh
lt --port 7777 --subdomain myhttp

如果成功,会显示:

sh
your url is: https://myhttp.loca.lt -> http://localhost:7777

小伙伴访问 https://myhttp.loca.lt 就能访问你本地 3000 端口的 HTTP 服务。

密码就是开启服务的电脑公网 ip


5. 暴露 WebSocket 服务

再开一个终端窗口,执行:

sh
lt --port 18023 --subdomain myws

会显示:

sh
your url is: https://myws.loca.lt -> http://localhost:18023

小伙伴访问 wss://myws.loca.lt 就能连到你本地的 WebSocket 服务。

Ngrok

1. 下载 ngrok: https://ngrok.com/download

2. 登录官网拿到 authtoken

sh
# 对 ngrok 代理进行身份验证。这会将您的 Authtoken 保存到默认配置文件中,因此您只需执行一次。
ngrok config add-authtoken 32jeUExqzxLfN1tgep9S1gD8Y7n_CXtTku8T87p2jdPg1dok

3. 点击 exe 文件

sh

# 运行
ngrok http 18023

# 会显示类似这样的输出:
# Forwarding  https://abc123.ngrok.io -> http://localhost:18023

如有转载或 CV 的请标注本站原文地址

访客数 --| 总访问量 --