Skip to content

TCP和HTTP连接配置

通过配置文件的方式设置TCPHTTP连接

服务端配置

启动ngrok服务端应用,指定证书、域名、http/https端口以及隧道端口

./ngrokd -tlsKey=a.key -tlsCrt=a.pem -domain="xxx.xxx.xxx" -httpAddr=":port1" -httpsAddr=":port2" -tunnelAddr=":port3"
  • 输入域名应该和证书匹配,当前使用二级域名xxx.xxx.xxx
  • 端口号的设置方式为:port
    • http端口默认为80
    • https端口默认为443
    • 隧道端口默认为4443

客户端配置

通过配置文件方式来完成HTTPTCP连接

配置文件

ngrok使用的配置文件是YAML格式,新建ngrok.cfg

server_addr: DOMAIN_NAME:TUNNEL_PORT
trust_host_root_certs: true
tunnels:
  tunnel-1:
    subdomain: SUB_DOMAIN 
    remote_port: REMOTE_PORT
    proto:
      http: LOCAL_PORT
  tunnel-2:
    remote_port: REMOTE_PORT
    proto:
      tcp: LOCAL_PORT
  • server_addr:指定远程服务器地址以及隧道端口
  • trust_host_root_certs:如果使用自签名证书,设为false
  • tunnels:设置要连接的服务(TCP/HTTP/HTTPs)

当前设置了两个服务:tunnel-1tunnel-2tunnel-1设置了http连接,tunnel-2设置了tcp连接

  • subdomain:指定子域名。注意:http服务均会设置子域名,如果没有设置subdomain属性,将会使用tunnel-1作为子域名
  • remote_port:远程绑定的端口号
  • proto:指定当前协议和本地端口号

使用

启动指定服务

# 启动http
$ ./ngrok -config ngrok.cfg start tunnel-1
# 启动tcp
$ ./ngrok -config ngrok.cfg start tunnel-2

启动所有服务

$ ./ngrok -config ngrok.cfg start-all

注意:http连接得到了三级域名(xxx.xxx.xxx.xxx),需要额外进行DNS解析

相关阅读