This document uses a project that actually went live as an example to give a brief introduction to
Ngrok. I hope readers can draw inferences — after all, you need to practice on concrete projects yourself to deepen the experience.
Below I will cover ‘an overview of NAT traversal’, ‘ngrok principles and use cases’, ‘the team’s needs’, ‘compiling and using ngrok’, and ‘using ngrok together with Nginx’. Readers who already understand the principles can jump straight to ‘compiling and using ngrok’ and what follows.
Overview of NAT Traversal
When we start talking about NAT traversal, I want to first introduce what an intranet is, and why we normally use intranets. Let’s look at a diagram first.

When you visit http://www.baidu.com, our destination is that website, but during the network request we first pass
through a NAT server — of course there may be more than one such server, depending on the ISP.
ngrok Principles and Use Cases
The Team’s Needs
As a campus team, we have always been committed to providing students with high-quality services.
Many services that require the campus network are aggregated on our intranet servers, exposed as RESTful interfaces. Once these externally exposed servers hit certain special circumstances, they are likely to be affected and unable to serve normally. This kind of force majeure isn’t aimed at our system specifically, but it does cause considerable inconvenience in some respects.
Fortunately such situations are rare, and servers inside the campus network can access the network outside — this gives us an idea for solving the problem: with NAT traversal we can map certain services to the external network as a temporary fix.
Preparation
Make sure you have your own domain and a public IP. The rest of this article uses the domain myseu.cn as an example.
Adding Domain A Records
Ngrokneeds a service established on the public network, so intranet servers need to resolve the domain to find the serverIP.
You need to add two domain A records (both pointing to your public server IP):
| A record | Purpose |
|---|---|
ngrok.myseu.cn |
client finds the server |
*.ngrok.myseu.cn |
provides subdomain services |
Downloading and Compiling ngrok
A lot of code follows. The purpose of much of it may not be obvious; if you’re interested, see the references at the end — the English version explains every step in detail.
Download:
1 | git clone https://github.com/inconshreveable/ngrok.git ngrok |
Compile:
1 | openssl genrsa -out rootCA.key 2048 |
Check the result:
1 | ☁ ngrok [master] ⚡ cd bin |
Simple Deployment
Deployment covers the server side and the client side.
First deploy the server:
- Copy the files to the server’s
/tmpdirectory
- Copy the files to the server’s
1 | ☁ ngrok [master] ⚡scp bin/ngrokd heraldnew:/tmp |
- Log into the server and start listening
1 | ➜ /tmp ./ngrokd -tlsKey=snakeoil.key -tlsCrt=snakeoil.crt -domain="ngrok.myseu.cn" -tunnelAddr=":5555" -httpAddr=":8988" -httpsAddr=":8989" |
Deploy the client:
- Create a config file (see the references for config files): ngrok.cfg
1 | server_addr: "ngrok.myseu.cn:5555" |
- Establish the connection
1 | ☁ bin [master] ⚡ ./ngrok -subdomain he -config=ngrok.cfg 80 |
The example maps the local port 80. That concludes intranet forwarding; next we need to configure the public server.
Using ngrok with Nginx
If our goal were only port forwarding, the work would be done. But we need to provide an external API, reverse-proxied by the public server’s
Nginx— that is, we are still oneNginxaway from really serving traffic.
Suppose we use a direct reverse proxy like this:
1 | location /api { |
Be sure to test before going live. Testing reveals this problem when requesting the API: Tunnel xxx not found.
The Nginx configuration must not be this simple. When proxying, it is actually Nginx making requests to the backend,
and the request headers get filled in automatically — the backend can no longer recognize them. We need to modify them
manually to restore their original state:
1 | location /api { |
References:
Run Ngrok on Your Own Server Using Self-Signed SSL Certificate Building your own ngrok service Notes on using ngrok
LICENSE:
Copyright © 2017 corvo. Commercial use without permission is prohibited. Please credit the source when reposting.