美文网首页
CentOS7搭建ngrok服务器

CentOS7搭建ngrok服务器

作者: 琴声悠扬TODO | 来源:发表于2017-09-14 14:07 被阅读691次

    准备

    提示

    安装GCC

    yum install gcc
    

    安装GIT

    yum install git
    

    安装GO环境

    wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz
    tar -C /usr/local/ -zxvf go1.8.linux-amd64.tar.gz
    

    vi /etc/profile最后添加

    export GOROOT=/usr/local/go
    export PATH=$PATH:$GOROOT/bin
    

    使环境变量生效&测试

    source /etc/profile
    go version
    #输出:go version go1.8 linux/amd64表示安装成功
    

    在服务器上搭建Ngrok服务

    1.下载ngrok源码

    cd /usr/local/src
    git clone https://github.com/inconshreveable/ngrok.git
    

    2.生成证书

    cd /usr/local/src/ngrok
    
    export NGROK_DOMAIN="ng.lvkerry.com"
    
    openssl genrsa -out rootCA.key 2048
    openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
    openssl genrsa -out device.key 2048
    openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
    openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
    

    将新生成的证书,替换掉assets/client/tls下的证书

    cp rootCA.pem assets/client/tls/ngrokroot.crt
    cp device.crt assets/server/tls/snakeoil.crt
    cp device.key assets/server/tls/snakeoil.key
    

    3.编译生成ngrokd(服务端)

    #这里是交叉编译,linux系统GOOS=linux,64位系统GOARCH=amd64,32位系统GOARCH=386
    #当前系统可用go env查看
    cd /usr/local/src/ngrok
    GOOS=linux GOARCH=amd64
    make release-server
    

    问题

    问题1

    [root@jdu4e00u53f7 src]# cd /usr/local/go/src
    [root@jdu4e00u53f7 src]# GOOS=windows GOARCH=amd64 ./make.bash
    ##### Building Go bootstrap tool.
    cmd/dist
    ERROR: $GOROOT_BOOTSTRAP must not be set to $GOROOT
    Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
    

    解决办法

    命令行输入.指定到Go1.4目录

    wget https://storage.googleapis.com/golang/go1.4.src.tar.gz
    tar -C /home/go1.4 -zxvf go1.4.src.tar.gz
    export GOROOT_BOOTSTRAP=/home/go1.4
    

    问题2

    Tunnel Status                 online
    Version                       1.7/1.7
    Forwarding                    http://test. -> 127.0.0.1:8011
    Forwarding                    https://test. -> 127.0.0.1:8011
    Web Interface                 127.0.0.1:4040
    # Conn                        0
    Avg Conn Time                 0.00ms
    

    在启动时域名写上-domain="ng.lvkerry.com",不用"$NGROK_DOMAIN"

    ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="ng.lvkerry.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"

    服务器启动

    ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="ng.lvkerry.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"

    后台运行
    nohup ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="ng.lvkerry.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083" &

    3.其他问题(看参考)

    客户端

    Window32位

    cd /usr/local/go/src
    GOOS=windows GOARCH=386 ./make.bash
    GOOS=windows GOARCH=386 make release-client
    

    Windows客户端64

    cd /usr/local/go/src
    GOOS=windows GOARCH=amd64 ./make.bash
    cd /usr/local/ngrok
    GOOS=windows GOARCH=amd64 make release-client
    

    Linux客户端64

    cd /usr/local/go/src
    GOOS=linux GOARCH=amd64 ./make.bash
    cd /usr/local/ngrok
    GOOS=linux GOARCH=amd64 make release-client
    

    客户端配置文件:ngrok.cfg

    PS:不能用TAB,缩进只能用空格

    PS:不能用TAB,缩进只能用空格

    PS:不能用TAB,缩进只能用空格

    server_addr: "ng.lvkerry.com:8083"
    trust_host_root_certs: false
    tunnels:
      http:
        subdomain: "ng"
        proto:
          http: "8090"
          
      https:
        subdomain: "www"
        proto:
          https: "8091"
      ssh:
        remote_port: 2222
        proto:
          tcp: "22"
      gw:
        remote_port: 15440
        proto:
          tcp: "15440"
        
    

    客户端启动:

    ngrok.exe -config ngrok.cfg start http https ssh gw
    

    gw,ssh.等名字随意

    域名解析

    添加A记录

    ng 122.122.12.12
    

    添加CNAME记录

    *.ng  ng.lvkerry.com
    

    *.ng.lvkerry.com都映射到ng.lvkerry.com

    Nginx去掉端口

    我好像测试一个Ngrok只能一个Http,TCP可以多个

    nginx配置文件中加入

    8081:为ngrok启动的Http端口

    server {
        listen 80;
        server_name "hexo.ng.lvkerry.com";
    
        location / {
    
            proxy_set_header X-Real-IP $remote_addr;
    
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            proxy_set_header Host  $http_host:8081;
    
            proxy_set_header X-Nginx-Proxy true;
    
            proxy_set_header Connection "";
    
            proxy_pass     http://localhost:8081/;
    
        }
    

    相关文章

      网友评论

          本文标题:CentOS7搭建ngrok服务器

          本文链接:https://www.haomeiwen.com/subject/llldsxtx.html