ngrok 通过外网链接映射到本地机器,支持http,https

说明

本地服务器一般用http://localhost:8080 来访问,只能通过本地自己访问。别人可以访问链接么,正常情况下是不可以的,防火墙等等限制。
本文的主角ngrok 就是救场的英雄。可以映射到本地机器来访问,可以通过http,也可以通过https.

用法

详细可以通过官网链接设置,笔者也把步骤罗列一下。笔者为Mac OS.
https://dashboard.ngrok.com/get-started/setup

1. 下载安装包, 并解压(可以双击解压,也可用通过如下命令)。

unzip /path/to/ngrok.zip

正常情况下,cd到可执行文件目录,运行ngrok 命令就好。为了方便,笔者用myzsh 设置环境变量

$ cd $home
$ vim .zshrc
// 添加如下 信息 到环境变量
# ngrok
export NGROK_HOME="/Users/yourpath/software/ngrok"
export PATH=$NGROK_HOME:$PATH

// esc > :wq 保存
// 重新加载 配置文件
source .zshrc

笔者该坏过配置文件,这也没有问题,文件夹打开根目录,显示隐藏文件快捷键为command+shift+. , 隐藏隐藏文件为再次用同样的命令。用文本工具比如Sublime编辑好,保存,在命令行里重新加载配置文件就好。

ngrok
NAME:
   ngrok - tunnel local ports to public URLs and inspect traffic

DESCRIPTION:
    ngrok exposes local networked services behinds NATs and firewalls to the
    public internet over a secure tunnel. Share local websites, build/test
    webhook consumers and self-host personal services.
    Detailed help for each command is available with 'ngrok help <command>'.
    Open http://localhost:4040 for ngrok's web interface to inspect traffic.

EXAMPLES:
    ngrok http 80                    # secure public URL for port 80 web server
    ngrok http -subdomain=baz 8080   # port 8080 available at baz.ngrok.io
    ngrok http foo.dev:80            # tunnel to host:port instead of localhost
    ngrok http https://localhost     # expose a local https server
    ngrok tcp 22                     # tunnel arbitrary TCP traffic to port 22
    ngrok tls -hostname=foo.com 443  # TLS traffic for foo.com to port 443
    ngrok start foo bar baz          # start tunnels from the configuration file

VERSION:
   2.3.35

AUTHOR:
  inconshreveable - <alan@ngrok.com>

COMMANDS:
   authtoken	save authtoken to configuration file
   credits	prints author and licensing information
   http		start an HTTP tunnel
   start	start tunnels by name from the configuration file
   tcp		start a TCP tunnel
   tls		start a TLS tunnel
   update	update ngrok to the latest version
   version	print the version string
   help		Shows a list of commands or help for one command

2. 绑定ngroktoken

./ngrok authtoken 1cz..yourtoken.X6

token查找位置
在这里插入图片描述

3. 映射本地8080端口为远程端口

ngrok http 8080 

命令行里面显示了访问的记录。这里说明一下,需要梯子才能访问(⊙x⊙;)。
在这里插入图片描述
笔者本地访问
在这里插入图片描述
笔者用ngrok的域名访问
在这里插入图片描述

Bravo!!穿越成功🎉🎉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章