How to use the command 'ngrok' (with examples)
The ’ngrok’ command is a reverse proxy tool that creates a secure tunnel from a public endpoint to a locally running web service. It allows you to expose your local services to the internet, making them accessible to anyone.
Use case 1: Expose a local HTTP service on a given port
Code:
ngrok http 80
Motivation: You may want to expose a local HTTP service running on port 80 to the internet. This is useful for testing or sharing a web application with others without having to deploy it to a publicly accessible server.
Explanation:
- ’ngrok’ is the command to run ngrok.
- ‘http’ specifies the protocol to use for the tunnel.
- ‘80’ is the port number of the local HTTP service you want to expose.
Example output:
Session Status online
Account John Doe (Plan: Free)
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://abcdef.ngrok.io -> http://localhost:80
Use case 2: Expose a local HTTP service on a specific host
Code:
ngrok http foo.dev:80
Motivation: Sometimes, you may have a web application that is configured to only accept requests from a specific host. By using ngrok, you can expose your local HTTP service on a specific host, allowing it to communicate with the application.
Explanation:
- ‘foo.dev:80’ is the host and port of the local HTTP service you want to expose. Replace ‘foo.dev’ with the actual host name.
Example output:
Session Status online
Account John Doe (Plan: Free)
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://abcdef.ngrok.io -> http://foo.dev:80
Use case 3: Expose a local HTTPS server
Code:
ngrok http https://localhost
Motivation: If you have a local HTTPS server running on your machine, you may want to expose it to the internet for testing or other purposes. ngrok allows you to create a secure tunnel for your HTTPS server, making it accessible over the internet.
Explanation:
- ‘https://localhost’ is the URL of the local HTTPS server you want to expose. Replace ’localhost’ with the actual hostname or IP address.
Example output:
Session Status online
Account John Doe (Plan: Free)
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding https://abcdef.ngrok.io -> https://localhost
Use case 4: Expose TCP traffic on a given port
Code:
ngrok tcp 22
Motivation: You may have a TCP service running on a specific port, such as SSH (port 22), and want to access it from the internet. By using ngrok, you can create a secure tunnel for the TCP traffic, allowing you to connect to your local service remotely.
Explanation:
- ’tcp’ specifies the protocol to use for the tunnel.
- ‘22’ is the port number of the local TCP service you want to expose.
Example output:
Session Status online
Account John Doe (Plan: Free)
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding tcp://0.tcp.ngrok.io:12345 -> localhost:22
Use case 5: Expose TLS traffic for a specific host and port
Code:
ngrok tls -hostname=foo.com 443
Motivation: If you have a TLS (Transport Layer Security) service running on a specific host and port, you may want to expose it to the internet securely. ngrok allows you to create a secure tunnel for your TLS traffic, making it accessible over the internet.
Explanation:
- ’tls’ specifies the protocol to use for the tunnel.
- ‘-hostname=foo.com’ specifies the hostname of the TLS service you want to expose. Replace ‘foo.com’ with the actual hostname.
- ‘443’ is the port number of the local TLS service you want to expose.
Example output:
Session Status online
Account John Doe (Plan: Free)
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding tls://abcdef.ngrok.io -> foo.com:443
Conclusion:
The ’ngrok’ command is a powerful tool for exposing local services to the internet securely. Whether you have an HTTP, HTTPS, TCP, or TLS service, ngrok makes it easy to create a tunnel and access your local services remotely. With its simplicity and flexibility, ngrok is an essential tool for developers and anyone who needs to share or test web services.