概念用途
ngrok是非常流行的反向代理服务,可以进行内网穿透,支持80端口以及自定义tcp端口转发.
这样你就可以运行本地的程序,而让别人通过公网访问了.安装
1、下载, 解压
2、将可执行文件ngrok
移到系统的bin
文件夹内(或者将快捷链接移动到bin
目录下),注意是系统目录,不是用户目录.
3、只有这样,才可以在命令行执行ngrok
,而不会得到command not found
的错误信息.
使用说明
1、终端执行下面的命令 , 也就是监测web server的运行端口,默认是80 .
ngrok http 8080
这里因为我需要访问的是本地运行的tomcat,而它运行的端口是
8080
,当然你也可以改,不过一般默认即可,除非有端口冲突.
2、 如果没有报错,说明运行正常,这个时候,终端会出现下面的信息
ngrok by @inconshreveable (Ctrl+C to quit)Session Status onlineVersion 2.2.4Region United States (us)Web Interface http://127.0.0.1:4040Forwarding http://79ab83c9.ngrok.io -> localhost:8080Forwarding https://79ab83c9.ngrok.io -> localhost:8080Connections 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
页面上面查看到当前访问的请求细节.
image.png
错误信息是因为没有注册授权
5、 获取ngrok
的授权码,
6、 进行本机认证 Install your authtoken.
ngrok authtoken
7、设置外网的访问密码
// username 用户名 password密码 8080相关的端口ngrok http -auth="username:password" 8080
8、 自定义子域名,(因为自动生成的会随机改变,如果想要固定访问域名地址,需要自定义), 这个功能需要付费!!!!!!!
// 举例,新开一个隧道,以`manajay` 为名称 ,后面跟Web端口ngrok http -subdomain=manajay 8080
9、 自定义域名
- 打开, 注册一个想要保留的域名
xxx.com
- 点击
CNAME
按钮,获取DNS CNAME record
,也就是后面的xxxx.ngrok.io
image.png
- 激活该域名
ngrok http -hostname=xxxx.com 8000
10、 关闭访问的监测
Disabling Inspectionngrok http -inspect=false '端口号'
11、 强制访问网址为Http
或者https
//httpngrok http -bind-tls=false site.dev:80// httpsngrok http -bind-tls=true site.dev:80
常见的报错
1. 免费用户不能自定义域名与子域名
// Custom subdomain namesFailed 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)
引用
作者:manajay 链接:https://www.jianshu.com/p/c273d3009ded 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。