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!!穿越成功🎉🎉

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