写在前面的话,2019年中旬自己开发了一个微信公众号,一直用的小米球的ngrok做内网穿透接口调试,免费的午餐终究吃不了太久。本着坚持勤俭持家的理念,让我购买付费业务是不可能的,索性自己在以前购买的云服务器搭建ngrok。nginx做端口转发
一台云服务器,一个域名,我用的服务器的操作系统为CentOS7(amd64)
#安装环境
安装gcc和git(用于下载ngrok源码)
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这里修改为自己的域名
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/目录中
GOOS=windows GOARCH=amd64 make release-client
生成在~/ngrok/bin/windows_amd64/目录中
将~/ngrok/bin/windows_amd64/里的文件下载到本地Windows下,如D:\ngrok
在D:\ngrok中新建文件,改名为 ngrok.cfg
文件中输入:
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
1、查看是否泛解析三级域名
2、生成的客户端的密钥是否一致
3、测试域名 端口是否都以开放