preface

Recently, due to the need of project development, we need to connect some interfaces with the third party. At first, it was intended to be directly deployed for interconnection on the public network. Later, it was found that repeated deployment was not conducive to debugging and wasted time. In the interconnection process, it would even be blocked in a certain place for a long time. Finally, we decided to use proxy mapping to the Intranet to facilitate debugging and interconnection.


Set up the premise

1. A public network server is required. This can be solved by renting a cloud server. 2. You need a domain name. I rent a.cn domain name for about 30 yuan a year.


Environment depends on

yum install build-essential golang mercurial git
git clonehttps://github.com/tutumcloud/ngrok.git ngrok / / install nginx you can also choose to compile yum install nginx/y/set an environment variable, hcyhj here. Cn is your domain name, the need to modifyexport NGROK_DOMAIN="hcyhj.cn"
Copy the code

Configuration ngrok

// Enter into youcloneDown in the ngrok directorycdNgrok // Execute the following command. 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. The CRT / / will be generated file copy past cp base. The pem assets/client/TLS/ngrokroot CRTCopy the code

Compile client and server

// Compile Linux server Linux client make release-server release-client // Compile Windows client GOOS= Windows GOARCH=amd64 make release-clientCopy the code

The compiled files will be stored in the ngrok/bin directory. As shown in figure:


Configuring Domain Name Resolution

This can be configured according to the guidance of different domain name vendors.


Nginx configuration file configuration

// Add the following configuration to nginx configuration file nginx.conf: server {listen 80; server_name *.hcyhj.cn hcyhj.cn ; 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: 8081; proxy_set_header X-Nginx-Proxytrue;
                        proxy_set_header Connection ""; Proxy_pass http://127.0.0.1:8081; }}Copy the code

Start nginx after configuration.


Start the ngrok

/ / note: /bin/ngrokd -tlskey =server.key -tlscrt =server.crt -domain="hcyhj.cn" -httpAddr=": 8081" -httpsAddr=": 8082"
Copy the code

Windows client

Download nginx.exe from the above location (ngrok.png) to Windows, create a new configuration file in the same directory, and name it whatever you want. For example ngrok. Conf.

// Add the code to the configuration file, where the domain name hcyhj.cn needs to be the same as the domain name configured in the above environment variables, port 4443, server nGROKD startup has a default communication port 4443, server_addr: Hcyhj.cn :4443 // Without SSL certificate trust_host_root_certs:false
Copy the code

Create a new bat file and add the code

// Note :subdomain is the domain name and port you want to usecd %cd%
ngrok -config=ngrok.conf -subdomain=www 8087 
Copy the code

Run the bat script to start the ngrok client. Then type www.hcyhj.cn in your browser (hcyhj.cn is your domain name) to redirect to your local application on port 8087. You can enable multiple applications. You only need to change the subdomain of the configuration file, for example, subdomain=aa 8081, to access the application that aa.hcyhj.cn maps to the local port 8081. And so on.


The little knowledge

Once deployed, the Ngrok server on Linux cannot run in the background. After Google found a solution, add the parameter -log at startup, for example:

nohup ./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="hcyhj.cn" -httpAddr=": 8081" -httpsAddr=": 8082" -log=stout &
Copy the code

End