Skip to content

[Linux] Using Ngrok To Set Up A Temporary Server And Forward The Port

Last Updated on 2022-09-12 by Clay

What is ngrok? What it can do?

Ngrok is a globally available "reverse proxy" tool that forwards your web services running on any device to a public URL.

In short, ngrok is usually not used for real website deployment, but to quickly serve our demo website.

Just imagine, today our laptop has opened a local web page service, which may be for customers to see, but how do we share it to customers for personal testing?

At this time, if we use ngrok, we can hand over the service port opened on the local side for ngrok to forward it, and ngork will automatically assign a URL to send our port is public; customers only need to enter the URL we publicized through ngrok in the browser, and they can test our web service by themselves.

So below, I will simply record how to use ngork!


How to download and use ngrok

Ngrok support different operating systems and installations, you can refer: https://ngrok.com/download

You can refer to the official instructions to download. By the way, in Linux (Such as Ubuntu/Debian and other distributions), you can actually go through:

sudo apt install ngrok


If there is no any ngrok tool in APT lists, you can try sanp.

sudo snap install ngrok


After that, you can go to https://dashboard.ngrok.com/get-started/setup to get your own token (if you are not a registered ngrok member, you must register. If you have, you can log in directly).

Open the terminal and key in:

接著在終端機中,輸入以下指令:

ngrok config add-authtoken [YOUR_TOKEN]


This is to allow ngrok to recognize your user identity when starting the proxy service.

After that, we can directly expose specific port using ngrok.

ngrok http [PORT]

Example

In here, I will try to show an example. I mkdir a share folder under /tmp, and use python to build a temporary server, finally use ngrok to forward the port of server.

Then, everybody can access the file I put there. (But I will close the ngrok after demo).

mkdir /tmp/share_folder/
cd /tmp/share_folder/
touch sample_01.txt sample_02.txt sample_03.txt
python3 -m http.server 18888


At this point, our temporary server has been set up, but only people who are in the same network space as me can get it. At this time, we can open another terminal and use ngrok to proxy the port 18888.

ngrok http 18888


Output:

The url http://99c2-61-222-146-139.jp.ngrok.io of the image is our temporary url.

Of course, the url generated by ngrok can be accessed as long as it is on the public network, and it does not need to be on the same network as the device where I set up the server.

So for many use cases, ngrok is a convenient tool for NAT traversal.

But in theory, as long as you have a server with a fixed IP, you can use it as a reverse proxy.

Regarding the method of reverse proxy, I plan to write another record in the future.


References


Read More

Leave a Reply