<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Reverse Proxy | Ziyang Lin</title><link>https://ziyanglin.netlify.app/en/tags/reverse-proxy/</link><atom:link href="https://ziyanglin.netlify.app/en/tags/reverse-proxy/index.xml" rel="self" type="application/rss+xml"/><description>Reverse Proxy</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><lastBuildDate>Fri, 27 Jun 2025 01:00:00 +0000</lastBuildDate><image><url>https://ziyanglin.netlify.app/img/icon-192.png</url><title>Reverse Proxy</title><link>https://ziyanglin.netlify.app/en/tags/reverse-proxy/</link></image><item><title>ngrok Technical Guide: Public Network Mapping and Tunneling for Local Services</title><link>https://ziyanglin.netlify.app/en/post/ngrok-documentation/</link><pubDate>Fri, 27 Jun 2025 01:00:00 +0000</pubDate><guid>https://ziyanglin.netlify.app/en/post/ngrok-documentation/</guid><description>&lt;h2 id="1-introduction">1. Introduction&lt;/h2>
&lt;h3 id="11-what-is-ngrok">1.1 What is ngrok?&lt;/h3>
&lt;p>ngrok is a powerful reverse proxy tool that can expose your local development environment to the public internet. By creating a secure tunnel, ngrok can forward requests from the public internet to services running on your local machine. This makes it exceptionally easy to integrate with external services (such as Webhooks, APIs) during development and testing phases.&lt;/p>
&lt;h3 id="12-how-it-works">1.2 How It Works&lt;/h3>
&lt;p>The working principle of ngrok can be summarized in the following steps:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Start the ngrok client&lt;/strong>: You run the ngrok client on your local machine and specify the local port to expose.&lt;/li>
&lt;li>&lt;strong>Establish a secure tunnel&lt;/strong>: The ngrok client connects to the ngrok cloud service and establishes a secure encrypted tunnel.&lt;/li>
&lt;li>&lt;strong>Assign a public address&lt;/strong>: The ngrok cloud service assigns you a unique public URL (e.g., &lt;code>https://random-string.ngrok.io&lt;/code>).&lt;/li>
&lt;li>&lt;strong>Forward requests&lt;/strong>: When requests are sent to this public URL, the ngrok cloud service forwards them through the tunnel to your local ngrok client.&lt;/li>
&lt;li>&lt;strong>Access local service&lt;/strong>: The ngrok client then forwards the requests to the service running on the specified local port.&lt;/li>
&lt;/ol>
&lt;pre>&lt;code class="language-mermaid">sequenceDiagram
participant User as User/External Service
participant NgrokCloud as ngrok Cloud Service
participant NgrokClient as Local ngrok Client
participant LocalServer as Local Web Service
User-&amp;gt;&amp;gt;NgrokCloud: Request https://&amp;lt;subdomain&amp;gt;.ngrok.io
NgrokCloud-&amp;gt;&amp;gt;NgrokClient: Forward request through secure tunnel
NgrokClient-&amp;gt;&amp;gt;LocalServer: Request http://localhost:&amp;lt;port&amp;gt;
LocalServer--&amp;gt;&amp;gt;NgrokClient: Return response
NgrokClient--&amp;gt;&amp;gt;NgrokCloud: Return response through secure tunnel
NgrokCloud--&amp;gt;&amp;gt;User: Return final response
&lt;/code>&lt;/pre>
&lt;h3 id="13-why-use-ngrok">1.3 Why Use ngrok?&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Webhook Development&lt;/strong>: Locally develop and test applications that need to receive webhooks (like GitHub, Stripe, Twilio).&lt;/li>
&lt;li>&lt;strong>API Testing&lt;/strong>: Allow mobile applications or other external services to access APIs you're developing locally.&lt;/li>
&lt;li>&lt;strong>Project Demonstration&lt;/strong>: Quickly demonstrate a website or application under development to clients or colleagues without deploying to a server.&lt;/li>
&lt;li>&lt;strong>Debugging&lt;/strong>: Capture and inspect all HTTP requests and responses through the tunnel for easy debugging.&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="2-quick-start">2. Quick Start&lt;/h2>
&lt;h3 id="21-download-and-installation">2.1 Download and Installation&lt;/h3>
&lt;ol>
&lt;li>&lt;strong>Visit the official website&lt;/strong>: Go to &lt;a href="https://ngrok.com/download">ngrok's official website&lt;/a>.&lt;/li>
&lt;li>&lt;strong>Download the client&lt;/strong>: Download the ngrok client corresponding to your operating system (Windows, macOS, Linux).&lt;/li>
&lt;li>&lt;strong>Extract the file&lt;/strong>: After downloading, extract the compressed package. You'll get an executable file named &lt;code>ngrok&lt;/code>.&lt;/li>
&lt;/ol>
&lt;h3 id="22-account-and-authtoken">2.2 Account and Authtoken&lt;/h3>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Register an account&lt;/strong>: Register a free account on &lt;a href="https://dashboard.ngrok.com/signup">ngrok's official website&lt;/a>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Get your Authtoken&lt;/strong>: After logging in, find your Authtoken on your &lt;a href="https://dashboard.ngrok.com/get-started/your-authtoken">dashboard&lt;/a> page.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Configure your Authtoken&lt;/strong>: Open a terminal, navigate to the directory containing the ngrok executable, and run the following command to add your Authtoken to the default configuration file &lt;code>ngrok.yml&lt;/code>:&lt;/p>
&lt;pre>&lt;code class="language-bash">./ngrok config add-authtoken &amp;lt;YOUR_AUTHTOKEN&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>After configuring your Authtoken, you'll be able to use more features, such as custom subdomains, longer session times, etc.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;h3 id="23-establish-your-first-tunnel">2.3 Establish Your First Tunnel&lt;/h3>
&lt;p>Suppose you have a Web service running on port &lt;code>8000&lt;/code> locally, you can use the following command to create a public tunnel for it:&lt;/p>
&lt;pre>&lt;code class="language-bash">./ngrok http 8000
&lt;/code>&lt;/pre>
&lt;p>After executing the command, you'll see output similar to the following in your terminal:&lt;/p>
&lt;pre>&lt;code>ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Account Your Name (Plan: Free)
Version 3.x.x
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding https://9a1b-2c3d-4e5f-6a7b-8c9d.ngrok.io -&amp;gt; http://localhost:8000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
&lt;/code>&lt;/pre>
&lt;p>Now, you can access your local service on port &lt;code>8000&lt;/code> through the public address &lt;code>https://9a1b-2c3d-4e5f-6a7b-8c9d.ngrok.io&lt;/code>.&lt;/p>
&lt;p>At the same time, you can also access ngrok's Web interface by visiting &lt;code>http://127.0.0.1:4040&lt;/code> in your browser, where you can view details of all requests and responses through the tunnel.&lt;/p>
&lt;hr>
&lt;h2 id="3-core-concepts">3. Core Concepts&lt;/h2>
&lt;h3 id="31-tunnel-protocols">3.1 Tunnel Protocols&lt;/h3>
&lt;p>ngrok supports multiple protocols for creating tunnels:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>HTTP/HTTPS&lt;/strong>: The most commonly used protocol for exposing Web services.&lt;/p>
&lt;pre>&lt;code class="language-bash"># Expose HTTP service on local port 80
ngrok http 80
# Expose HTTPS service on local port 3000
ngrok http https://localhost:3000
&lt;/code>&lt;/pre>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>TCP&lt;/strong>: Used to expose non-HTTP services, such as SSH, database connections, game servers, etc.&lt;/p>
&lt;pre>&lt;code class="language-bash"># Expose SSH service on local port 22
ngrok tcp 22
&lt;/code>&lt;/pre>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>TLS&lt;/strong>: Used to expose TCP services that require end-to-end TLS encryption.&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok tls --domain=your-domain.com 443
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="32-custom-domains">3.2 Custom Domains&lt;/h3>
&lt;p>For paid users, ngrok allows you to use custom subdomains or fully custom domains.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>Custom subdomain&lt;/strong>:&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok http --subdomain=my-awesome-app 8080
&lt;/code>&lt;/pre>
&lt;p>This will expose your service at &lt;code>https://my-awesome-app.ngrok.io&lt;/code>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Custom domain&lt;/strong> (requires a paid plan and CNAME configuration):&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok http --hostname=dev.example.com 80
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="4-advanced-usage">4. Advanced Usage&lt;/h2>
&lt;h3 id="41-configuration-file">4.1 Configuration File&lt;/h3>
&lt;p>In addition to specifying parameters on the command line, you can also define tunnels through the &lt;code>ngrok.yml&lt;/code> configuration file. This is very useful for managing multiple tunnels and complex configurations.&lt;/p>
&lt;p>By default, the configuration file is located at:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>macOS&lt;/strong>: &lt;code>~/Library/Application Support/ngrok/ngrok.yml&lt;/code>&lt;/li>
&lt;li>&lt;strong>Linux&lt;/strong>: &lt;code>~/.config/ngrok/ngrok.yml&lt;/code>&lt;/li>
&lt;li>&lt;strong>Windows&lt;/strong>: &lt;code>C:\Users\YourUser\AppData\Local\ngrok\ngrok.yml&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>An example of a configuration file:&lt;/p>
&lt;pre>&lt;code class="language-yaml">version: &amp;quot;2&amp;quot;
authtoken: &amp;lt;YOUR_AUTHTOKEN&amp;gt;
tunnels:
my-api:
proto: http
addr: 8080
subdomain: my-cool-api
ssh:
proto: tcp
addr: 22
&lt;/code>&lt;/pre>
&lt;p>After configuration, you can start tunnels by name:&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok start my-api
ngrok start ssh
ngrok start --all # Start all defined tunnels
&lt;/code>&lt;/pre>
&lt;h3 id="42-security-options">4.2 Security Options&lt;/h3>
&lt;p>ngrok provides various security features to protect your tunnels:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>HTTP Basic Authentication&lt;/strong>: Add username and password protection to your tunnel.&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok http --basic-auth=&amp;quot;username:password&amp;quot; 8000
&lt;/code>&lt;/pre>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>OAuth 2.0&lt;/strong> (paid feature): Integrate with OAuth providers like Google, GitHub, Microsoft, etc., so that only authenticated users can access your tunnel.&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok http --oauth=google --oauth-allow-emails=user@example.com 8000
&lt;/code>&lt;/pre>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>IP Restrictions&lt;/strong> (paid feature): Only allow or deny access from specific IP addresses or CIDR ranges.&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok http --ip-restriction-allow-cidrs=203.0.113.0/24 8000
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="43-webhook-verification-paid-feature">4.3 Webhook Verification (paid feature)&lt;/h3>
&lt;p>ngrok can automatically verify signatures of webhook requests from certain services (like Twilio, Stripe), increasing security.&lt;/p>
&lt;pre>&lt;code class="language-bash">ngrok http --verify-webhook=twilio --verify-webhook-secret=&amp;lt;YOUR_SECRET&amp;gt; 8000
&lt;/code>&lt;/pre>
&lt;hr>
&lt;h2 id="5-api-and-integration">5. API and Integration&lt;/h2>
&lt;p>ngrok provides official client libraries that allow you to control tunnels programmatically. &lt;code>@ngrok/ngrok&lt;/code> is the official Node.js library.&lt;/p>
&lt;h3 id="51-installation">5.1 Installation&lt;/h3>
&lt;pre>&lt;code class="language-bash">npm install @ngrok/ngrok
&lt;/code>&lt;/pre>
&lt;h3 id="52-example-starting-a-tunnel-in-a-nodejs-application">5.2 Example: Starting a Tunnel in a Node.js Application&lt;/h3>
&lt;pre>&lt;code class="language-javascript">const ngrok = require(&amp;quot;@ngrok/ngrok&amp;quot;);
// Set up Express application
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) =&amp;gt; {
res.send('Hello from local server!');
});
app.listen(port, async () =&amp;gt; {
console.log(`Local server listening at http://localhost:${port}`);
// Start ngrok tunnel
try {
const listener = await ngrok.forward({
addr: port,
authtoken_from_env: true, // Read from NGROK_AUTHTOKEN environment variable
});
console.log(`Ingress established at: ${listener.url()}`);
} catch (error) {
console.error(&amp;quot;Error establishing ngrok tunnel:&amp;quot;, error);
}
});
&lt;/code>&lt;/pre>
&lt;hr>
&lt;h2 id="6-frequently-asked-questions-faq">6. Frequently Asked Questions (FAQ)&lt;/h2>
&lt;p>&lt;strong>Q: Is the ngrok tunnel address fixed?&lt;/strong>
A: In the free plan, you'll get a new random URL each time you restart the ngrok client. Paid plan users can use fixed subdomains or custom domains.&lt;/p>
&lt;p>&lt;strong>Q: How do I run ngrok in the background?&lt;/strong>
A: On Linux or macOS, you can use &lt;code>&amp;amp;&lt;/code> to place it in the background: &lt;code>./ngrok http 8000 &amp;amp;&lt;/code>. For more stable solutions, it's recommended to use tools like &lt;code>systemd&lt;/code> or &lt;code>supervisor&lt;/code> to manage the ngrok process.&lt;/p>
&lt;p>&lt;strong>Q: What are the main differences between the free and paid versions?&lt;/strong>
A: The paid version offers more advanced features, including:&lt;/p>
&lt;ul>
&lt;li>Custom/fixed subdomains&lt;/li>
&lt;li>Custom domains&lt;/li>
&lt;li>More concurrent tunnels&lt;/li>
&lt;li>IP whitelisting/blacklisting&lt;/li>
&lt;li>OAuth integration&lt;/li>
&lt;li>Longer session timeout times&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Q: Can I run multiple tunnels simultaneously?&lt;/strong>
A: Yes. You can define multiple tunnels in the configuration file and start them with &lt;code>ngrok start --all&lt;/code>, or open multiple terminal windows and run the &lt;code>ngrok&lt;/code> command separately. The free version has limitations on the number of concurrent tunnels.&lt;/p></description></item></channel></rss>