[TOC]node
寫在前面的話,2019年中旬本身開發了一個微信公衆號,一直用的小米球的ngrok作內網穿透接口調試,免費的午飯終究吃不了過久。本着堅持勤儉持家的理念,讓我購買付費業務是不可能的,索性本身在之前購買的雲服務器搭建ngrok。nginx作端口轉發
一臺雲服務器,一個域名,我用的服務器的操做系統爲CentOS7(amd64)linux
安裝gcc和git(用於下載ngrok源碼)nginx
yum install gcc -y yum install git -y
yum install -y mercurial git bzr subversion golang golang-pkg-windows-amd64 golang-pkg-windows-386
git --version //( >= 1.7 ) go version
git clone https://github.com/inconshreveable/ngrok.git
cd ngrok
abc.com這裏修改成本身的域名git
export NGROK_DOMAIN="abc.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
cp -y rootCA.pem assets/client/tls/ngrokroot.crt cp -y device.crt assets/server/tls/snakeoil.crt cp -y device.key assets/server/tls/snakeoil.key
GOOS=linux GOARCH=amd64 make release-server
生成在~/ngrok/bin/目錄中github
GOOS=windows GOARCH=amd64 make release-client
生成在~/ngrok/bin/windows_amd64/目錄中golang
將~/ngrok/bin/windows_amd64/裏的文件下載到本地Windows下,如D:ngrokweb
在D:ngrok中新建文件,更名爲 ngrok.cfg
文件中輸入:windows
server_addr: "ngrok.abc.com:8083" trust_host_root_certs: false tunnels: http: subdomain: "www" proto: http: "80" https: subdomain: "www" proto: https: "443" ssh: remote_port: 2222 proto: tcp: "22" mstsc: remote_port: 52222 proto: tcp: "192.168.0.107:3389"
以上 8083 80 443 與 遠程開啓的端口一致,安全
注意:查看本身雲服務器安全組
是否開放端口,還有防火牆是否開放端口(這裏我卡了很久)服務器
在ngrok目錄中
若是不在
cd ngrok
sudo ./bin/ngrokd -domain="abc.com" -httpAddr=":80" -httpsAddr=":443" -tunnelAddr=":8083" &
這裏的端口號與config文件對應,視狀況而定
[16:05:23 CST 2018/01/09][INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry][tun] No affinity cache specified [16:05:23 CST 2018/01/09][INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:80 [16:05:23 CST 2018/01/09][INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443 [16:05:23 CST 2018/01/09][INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:8083 [16:05:23 CST 2018/01/09][INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
顯示此爲成功開啓
在Windows中D:\ngrok
新建文件 更名 start.bat
輸入:
ngrok -config=ngrok.cfg start http https ssh mstsc
直接雙擊運行
以前個人雲服務搭建多其它web應用,若是不用80端口轉發,那微信後臺用不了,解決辦法nginx作端口轉發
修改端口 啓動ngrok
~/ngrok/bin/ngrokd -domain="ngrok.abc.com" -httpAddr=":8080" -httpsAddr=":8443" -tunnelAddr=":4443" &
nginx配置
server { listen 80; server_name *.ngrok.abc.com ngrok.abc.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:8080; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_pass http://127.0.0.1:8080; } }
訪問www.ngrok.abc.com
這樣就能夠轉發到www.ngrok.abc.com:8080
一、查看是否泛解析三級域名
二、生成的客戶端的密鑰是否一致
三、測試域名 端口是否都以開放
本文由博客一文多發平臺 OpenWrite 發佈!