首先需要准备有固定外网IP的主机和域名,假设使用域名test.qiujinwu.com作为提供服务的域名,那么先到dns解析方添加【A记录】,注意包含【test】和【*.test】下面是dnspod的记录
git clone https://github.com/inconshreveable/ngrok.git
git clone https://github.com/tutumcloud/ngrok.git
sudo apt-get install build-essential golang mercurial git
ubuntu@VM-61-124-ubuntu:~$ go version
go version go1.2.1 linux/amd64
src/github.com/gorilla/websocket/client.go:361: unknown tls.Config field 'GetCertificate' in struct literal
src/github.com/gorilla/websocket/client.go:370: unknown tls.Config field 'ClientSessionCache' in struct literal
src/github.com/gorilla/websocket/client.go:373: unknown tls.Config field 'CurvePreferences' in struct literal
make: *** [client] Error 2
(venv) king@kingqiu:~/proxy/ngrok.bak$ go version
go version go1.7 linux/amd64
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=test.qiujinwu.com" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=test.qiujinwu.com" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key
sudo make release-server release-client
(venv) king@kingqiu:~/proxy/ngrok$ ls bin/
go-bindata ngrok ngrokd
sudo ./bin/ngrokd -domain="test.qiujinwu.com" -httpAddr=":80" -httpsAddr=":9082" -tunnelAddr=":4443"
server_addr: test.qiujinwu.com:4443
trust_host_root_certs: false
./ngrok -subdomain 【子域名qjw】-proto=http -config=./ngrok.cfg 【端口5000】
考虑一些特殊的需求,例如微信必须要求是80端口,但是服务器已经有其他程序占用了该端口,或者同时运行两个ngrok程序,但又必须分享80端口。这时就需要依赖nginx反向代理来支持
首先,反向代理有一个问题,如下,客户端代理链接包含了ngrok服务器绑定的本地端口(也就是nginx代理到的端口),这时直接访问80端口,ngrok服务器会提示找不到隧道,需要对nginx代理出来的http做一些处理,见下面的分析。
ngrok (Ctrl+C to quit)
Tunnel Status online
Version 1.7/1.7
Forwarding http://qjw.test.qiujinwu.com:8001 -> 127.0.0.1:8188
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
upstream ngrok1 {
server 127.0.0.1:8001;
keepalive 64;
}
server {
listen 80;
server_name *.test.qiujinwu.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:8001;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://ngrok1 ;
}
}
upstream ngrok {
server 127.0.0.1:8002;
keepalive 64;
}
server {
listen 80;
server_name *.test2.qiujinwu.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:8002;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://ngrok ;
}
}
#!/bin/bash
{
./ngrokd -domain="test.qiujinwu.com" -httpAddr=":8001" -tunnelAddr=":4443" -httpsAddr=":9082"
}&
{
./ngrokd_qiye -domain="test2.qiujinwu.com" -httpAddr=":8002" -tunnelAddr=":4444" -httpsAddr=":9083"
}&