22.04.2025

Ngrok: Installation and traffic policies

To set up your own server with a white address, you need to spend a lot of money: prepare a DNS name, buy out a white address or VPS/VDS, and set up NAT forwarding. And most importantly, it is not always clear how to maintain all this without a book on networks and network technologies. The creators of the Ngrok service solved this problem by providing their servers as public connection points, with all the benefits previously described!

What is Ngrok and how do I install it?

Ngrok is a server on a public network that already has a domain name configured in advance and has a static connection address that allows traffic to be redirected from it to your local device by tunneling. Schematically, it looks like this:

Screenshot №1 — Schema

The agent and service are installed on the local and public servers, respectively. After that, the agent connects to the Ngrok public point at points 2-3. Now they have an open connection that can be used to transfer traffic in two directions. A request is sent from the network to the web application. After that, according to the rules of redirection in the public node, number 4. Traffic is routed to the open connection of the service and gets to the agent, where it is unpacked and transmitted to the web application!

To set up a network connection, you need create an account on the official website and then copy the key!

Screenshot №2 — Key

We will use it to authenticate the agent in the reverse proxy, download ngrok through the snap manager.:

apt update && apt install snapd -y && \
echo “PATH=$PATH:/snap/bin” | tee -a ~/.bashrc && \
bash snap install ngrok

After that, we can create an endpoint in the control panel in the Gateway→Edges tab. You can use it to manage your reverse proxy server.:

Screenshot №3 — Create Endpoint

Now we will connect our local resource to it, for example, Nginx server, which occupies port 80.:

Screenshot №4 — Check port

To do this, add the previously copied key in the first step to the configuration file and open a tunnel from our local resource to a public point.:

ngrok config add-authtoken && \
ngrok tunnel --label edge=

Where the reverse proxy ID can be found in the panel on the Overview tab, and the URL represents the local resource to which access is required.

Screenshot №5 — Add Token

After that, we can see a panel with basic statistics on connections to the node.:

Screenshot №6 — Ngrok Panel

And by contacting a public point of sale, we will receive a response from the local web server for NAT:

Screenshot №7 — Nginx

How do I set up traffic policy in Ngrok?

In fact, this is the same module as the usual reverse proxy for managing network requests and routing paths. For example, using the /cat path to return the 403 code or redirect the user's request.

There are a total of 19 functions that can be implemented when receiving traffic.:

Screenshot №8 — Reverse Proxy Modules

Let's look at a few of them. Classically, you can write a config in YAML/JSON markup and point it to the agent, or make changes for each type of traffic immediately on the web platform.:

on_http_request:
- actions:
- type: custom-response
config:
status_code: 503
content: HTML code
headers:
content-type: text/html

After that, we will save and check the applied stub on the reverse proxy.:

curl -i https://example.ngrok.app/

Screenshot №9 — Error 503

Great! The stub worked perfectly and gave the necessary result, in the same way you can apply actions on request/response for the rest of the modules, following the example! You can find out more about each of them in the official documentation.