反向代理(內網穿透)工具Ngrok安裝

ngrok是一個反向代理工具,1.x版本源碼開源;能夠本身搭建一個服務來使用,將本地的web或tcp服務經過公共端口和外部創建一個安全通道,這樣就能夠經過外網直接訪問本地對應的服務,在進行微信等測試開發的時候很是有用
html

安裝ngrok

環境

Linux Centos7 git golang opensslnode

安裝golang

一、下載golanglinux

wget https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz

默認在當前用戶目錄下
cd ~
lsnginx

二、解壓到/usr/local目錄git

tar -C /usr/local -xzf go1.11.4.linux-amd64.tar.gzgithub

三、添加環境變量golang

vim /etc/profile

在最後一行添加以下內容web

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

source /etc/profile 使環境變量生效vim

安裝ngrok

一、clone ngrok 到本地目錄 /usr/local/ngrokwindows

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

二、配置

cd /usr/local/ngrok

# 臨時設置域名 後面使用
export NGROK_DOMAIN="<youdomain>.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

# 將生成的證書替換ngrok默認的證書

# 先備份
cp assets/client/tls/ngrokroot.crt assets/client/tls/ngrokroot.crt.bak
cp assets/server/tls/snakeoil.crt assets/server/tls/snakeoil.crt.bak
cp assets/server/tls/snakeoil.key assets/server/tls/snakeoil.key.bak
# 替換
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key

三、編譯不一樣平臺的服務端和客戶端

# 編譯64位linux平臺服務端
GOOS=linux GOARCH=amd64 make release-server
# 編譯64位windows客戶端
GOOS=windows GOARCH=amd64 make release-client
# 若是是mac系統,GOOS=darwin。若是是32位,GOARCH=386

注:

  • Linux 平臺 32 位系統:GOOS=linux GOARCH=386
  • Linux 平臺 64 位系統:GOOS=linux GOARCH=amd64
  • Windows 平臺 32 位系統:GOOS=windows GOARCH=386
  • Windows 平臺 64 位系統:GOOS=windows GOARCH=amd64
  • MAC 平臺 32 位系統:GOOS=darwin GOARCH=386
  • MAC 平臺 64 位系統:GOOS=darwin GOARCH=amd64
  • ARM 平臺:GOOS=linux GOARCH=arm
  • 編譯的是不帶release的版本,還能夠經過-tlsCrt和 -tlsKey選項來指定證書文件的位置

執行後會在bin目錄及其子目錄下看到服務端ngrokdwindows客戶端windows_amd64/ngrok.exe

四、啓動服務端

./ngrokd  -domain="<youdomain>.com" -httpAddr=":10001"  -httpsAddr=":10002" -tunnelAddr=":10003"
  • 最後加 & 後臺運行
  • -domain 表示服務器域名
  • -httpAddr表示默認監聽的HTTP端口, 是訪問普通的http使用的端口號,用後面用 <subdomain>.<yourdomain>.com:10001 來訪問服務
  • -httpsAddr表示默認監聽的HTTPS端口,是訪問https使用的端口號,用後面用 <subdomain>.<yourdomain>.com:10002 來訪問服務
  • -tunnelAddr表示服務器監聽客戶端鏈接的隧道端口號,這個端口是Ngrok用來通訊的,因此這個端口在服務器上和客戶端上設置必需要對應才能夠正常的連接,默認不填寫是4433
  • -log表示日誌文件位置
  • -log-level用來控制日誌記錄的事件級別,選項有DEBUG、INFO、WARNING、ERROR
  • 防火牆端口開放 firewall-cmd firewall-cmd --permanent --zone=public --add-port=10001-10003/tcp

出現如下消息說明啓動成功

附錄1 參考文檔

https://luozm.github.io/ngrok
https://my.oschina.net/imjueying/blog/1786737
http://ngrok.cn/docs.html
http://www.imawen.com/content.html?id=26

附錄2 配置示例 YAML格式

server_addr: <youdomain>.com:10003
trust_host_root_certs: false
log: ngrok.log
tunnels:
  test:
    addr: 10001
    proto: http

附錄3 windows 啓動示例

須要在命令(cmd)行下執行,不能是直接點擊.exe文件運行

ngrok.exe -subdomain=test -config="ngrok.yml" 8080

訪問: http://test.<youdomain>.com:10003

通常 80 端口都是被使用了的,若也想讓 ngrok 使用 80 端口,能夠將客戶端 10001 改爲 80 再使用 nginx 做轉發

相關文章
相關標籤/搜索