Cosmo's Blog

Back

Ngrok 使用记录#

正好可以把工位的电脑当作这个后端服务器来弄那个卓越工程的事情, 不用占着我的电脑了hh

Ngrok#

ngrok 官方文档

点击查看

ngrok is a globally distributed reverse proxy that secures, protects, and accelerates your applications and network services.

You can think of ngrok as the front door to your applications.

ngrok is environment independent because it can deliver traffic to services running anywhere with no changes to your environment’s networking. Run your app on AWS, Azure, Heroku, an on-premise Kubernetes cluster, a Raspberry Pi, and even your laptop. With ngrok, it all works the same.

ngrok is a unified ingress platform because it combines all the components to deliver traffic from your services to the internet into one. ngrok consolidates together your reverse proxy, load balancer, API gateway, firewall, delivery network, DDoS protection and more.

ngrok 是一款开源的网络服务, 能够为在本地运行的网络应用提供公开的、基于互联网的 URL。简单的讲述工作原理就是:

  • 本地机器启动 ngrok, 指定一个或多个端口
  • ngrok 在本地机器上建立一个隧道, 连接到 ngrok 服务器
  • ngrok 服务器给该隧道分配一个公开 URL, 将所有发送至此 URL 的请求转发到本地机器
  • 本地应用程序处理请求并发送响应, 响应通过 ngrok 隧道返回到原始请求者

Ngrok 安装与配置#

打开 Ngrok 官网, 右上角 Log in 登录账号(使用 Github 或者 Google 账号登录), 进入其引导安装文档, 按照其提供步骤安装配置即可。

Windows 端可以直接用 Mircrosoft Store 安装, 也可以用其提供的可执行文件进行安装。安装完成后, 运行:

ngrok config add-authtoken your_authtoken
shell

your_authtoken 替换为你在 ngrok 官网注册账号后获取的 authtoken(在引导安装网页中直接给出了)

Ngrok 暴露端口(随机访问域名)#

运行以下命令暴露本地端口 8080 至公网:

ngrok http 8080
shell

ngrok 会输出一个公开的、随机的 URL, 访问该 URL 即可访问到本地端口 8080 提供的服务。

固定访问域名#

上述方法生成的访问 URL 是随机的, 不利于长期开发和协作, 可以在 ngrok dashboard 中找到提供的免费的固定域名

然后在暴露端口的时候指定域名即可:

ngrok http --url=your_domain 8080
shell
Ngrok 使用记录
https://astro-pure.js.org/blog/ngrok
Author Cosmo
Published at October 21, 2025