Oct 16

Ngrok可以使外网能够安全的访问内网Web主机,还支持SSH访问内网,上传或者下载文件比较方便,支持Mac OS X,Linux,Windows平台。

1.Ngrok下载运行
官网下载后直接解压得到一个二进制文件,在shell中执行./ngrok 80即可,默认会分配随机的二级域名来访问,转发到本机的80端口。可以通过-help参数来查看详细的说明,运行后如下提示:

Tunnel Status		online
Version			1.6/1.5
Forwarding		http://xxxxx.ngrok.com -> 127.0.0.1:8080
Forwarding		https://xxxxx.ngrok.com -> 127.0.0.1:8080
Web Interface		127.0.0.1:4040
# Conn			16
Avg Conn Time		558ms

这个随机的二级域名不便于记忆,可以修改成一个短小好记的,这是可以使用命令:

./ngrok -subdomain chu 80
./ngrok -subdomain=chu 80

2.tcp端口转发

意思就是可以在外网ssh到本机了,但是外网端口是随机分配的。

./ngrok -proto=tcp 22

外网登录命令为:

ssh root#chu.ngrok.com -p 34567

绑定顶级域名需要付费,有这个需求的可以购买,一年25美金。命令为:

./ngrok -hostname test.dorole.com 80

当然,还需要修改DNS记录才能正常访问。

3.Ngrok配置文件

有时候,不但需要访问内网网站,还需要SSH登录,这样的话命令就比较长,写一个配置文件比较方便。默认是放在~/.ngrok

auth_token: Cel40I5xxxxxxx
  tunnels:
    client:
       subdomain: "chu"
       proto:
         http: 80
         https: 80
     ssh:
       proto:
        tcp: 22

每一个隧道的配置节点都有五个参数,proto,subdomain,auth,hostname和remote_port,每个隧道必须有proto参数来指定本地地址和端口。auth参数用于在http(s)中身份认证,而remote_port用于在tcp隧道中指定远程服务器端口。如果没有配置subdomain参数,ngrok会默认一个二级域名与隧道节点一样的名字。

以上输入命令需要在前面加上./,如果觉得麻烦可以直接将ngrok文件放到/usr/bin文件夹,这样就不需要添加./了。当然,如果需要开机运行,还可以将它放到启动项里。

参考资料:
https://ngrok.com/
https://ngrok.com/usage
http://dorole.com/tag/ngrok/
http://lanvige.github.io/2013/12/02/test-the-web-locally-with-ngrok/

Leave a Reply