工慾善其事必先利其器
1.安裝 go
下載源代碼:https://golang.org/doc/install?download=go1.8.3.linux-amd64.tar.gz
解壓到 /usr/local
tar -zxvf go1.8.3.linux-amd64.tar.gz -C /usr/local
添加環境變量
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/bashrc
source /etc/bashrc
2.編譯 ngrok
首先我們設置 domain name,之後出現的所有 NGROK_DOMAIN 都需要替換成你自己的域名:
export NGROK_DOMAIN=your domain name
下載 ngrok 源碼:
git clone https://github.com/inconshreveable/ngrok.git
生成 RSA 文件:
NGROK_DOMAIN="your domain name"
openssl genrsa -out base.key 2048
openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=$NGROK_DOMAIN" -out base.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
openssl x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt
cp base.pem assets/client/tls/ngrokroot.crt
編譯:
make release-server release-client
編譯 Mac 客戶端
GOOS=darwin GOARCH=amd64 make release-server release-client
編譯 Windows 客戶端
GOOS=windows GOARCH=amd64 make release-client
將 bin/windows_amd64/ngrok.exe 下載到本地
3.啓動服務端
bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain=$NGROK_DOMAIN -httpAddr=":8081" -httpsAddr=":8082"
監聽了 3 個端口:8081, 8082, 4443
其中 4443 是客戶端和服務端通信用的
4.啓動客戶端
寫入配置文件 ngrok.cfg
server_addr: NGROK_DOMAIN:4443
trust_host_root_certs: false
ngrok -subdomain wxtest -proto=http -config=ngrok.cfg 8600
表示從客戶端過來的請求轉發到本地的 8600 端口
如果出現下面的界面,表示連接成功
5.配置 nginx 轉發
配置 nginx 文件 ngrok.conf
server {
listen 80;
server_name wxtest.NGROK_DOMAIN;
location / {
proxy_pass http://localhost:8081;
}
}
6.更改DNS
- wxtest.NGROK_DOMAIN A 記錄指向服務器 IP
- 把 wxtest.NGROK_DOMAIN 加入微信開發者的白名單
耶~~~
參考:
https://imququ.com/post/self-hosted-ngrokd.html (主要)
http://www.jhonge.com/Home/Single2/2482
http://www.rplees.com/2016/03/22/ngrok%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%90%AD%E5%BB%BA%E4%B8%8Elinux-mac-window%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84%E7%BC%96%E8%AF%91/
http://tonybai.com/2015/03/14/selfhost-ngrok-service/