正在加载今日诗词....
3 min read

ngrok - 内网穿透

ngrok - 内网穿透

概念用途

ngrok是非常流行的反向代理服务,可以进行内网穿透,支持80端口以及自定义tcp端口转发.
这样你就可以运行本地的程序,而让别人通过公网访问了.

安装

1、官网下载地址下载, 解压

2、将可执行文件ngrok移到系统的bin文件夹内(或者将快捷链接移动到bin目录下),注意是系统目录,不是用户目录.

3、只有这样,才可以在命令行执行ngrok,而不会得到command not found的错误信息.

使用说明

1、 If you don't know what port your web server is listening on, it's probably port 80, the default for HTTP. >>>>>终端执行下面的命令 , 也就是监测web server的运行端口,默认是80 .

ngrok http 8080

这里因为我需要访问的是本地运行的jenkins,而它运行的端口是8080,当然你也可以改,不过一般默认即可,除非有端口冲突.

2、 如果没有报错,说明运行正常,这个时候,终端会出现下面的信息

ngrok by @inconshreveable                                       (Ctrl+C to quit)

Session Status                online
Version                       2.2.4
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://79ab83c9.ngrok.io -> localhost:8080
Forwarding                    https://79ab83c9.ngrok.io -> localhost:8080

Connections                   ttl     opn     rt1     rt5     p50     p90
                              24      0       0.00    0.01    0.00    0.00

3、 浏览器打开 Web入口http://127.0.0.1:4040.

4、此时如果使用其他设备访问Forwarding后面的地址,就会在Web页面上面查看到当前访问的请求细节.

ngrok-page-38a45e6b-d9f4-4269-b511-5e116dd80eb6-1535521642339-10917663
错误信息是因为没有注册授权

5、 sign up for an account in ngrok 获取ngrok的授权码,

6、 进行本机认证 Install your authtoken.

ngrok authtoken <YOUR_AUTHTOKEN>

7、设置外网的访问密码

// username 用户名 password密码 8080相关的端口
ngrok http -auth="username:password" 8080

8、 自定义子域名,(因为自动生成的会随机改变,如果想要固定访问域名地址,需要自定义), 这个功能需要付费!!!!!!!

// 举例,新开一个隧道,以`manajay` 为名称 ,后面跟Web端口
ngrok http -subdomain=manajay 8080

9、 自定义域名

ngrok-cname-90d71a46-89a9-4b46-8384-b14b0594fea4-1535521626651-75362003

  • 激活该域名
ngrok http -hostname=xxxx.com 8000

10、 关闭访问的监测

Disabling Inspection
ngrok http -inspect=false '端口号'

11、 强制访问网址为Http或者https

//http
ngrok http -bind-tls=false site.dev:80
// https
ngrok http -bind-tls=true  site.dev:80

常见的报错

1. 免费用户不能自定义域名与子域名
// Custom subdomain names
Failed to bind the custom subdomain 'manajay' for the account 'manajay'. This account is on the 'Free' plan.
// Tunnels on custom domains (white label URLs)
2. 设置访问密码
// auth出错
ngrok http -auth="username:password" 8080
//
Tunnel session failed: Your account '' is limited to 1 simultaneous ngrok client session.
Active ngrok client sessions in region 'us':
  - xxxxxx (xxxxx:58075)

参考链接