HTTP/S Endpoints
Overview
HTTP/S endpoints enable you to serve web services like REST APIs, web
applications, websites and websocket servers. Serving a web application is as
simple as ngrok http 80
.
Once your endpoint is running, check out:
- Traffic Policy - Add routing, authentication and traffic transformation
- Traffic Inspector - Real-time observability with request/response introspection
- Endpoint Pooling - Load balancing
Quickstart
Agent Endpoint
Agent Endpoints are the easiest way to get started with ngrok. An agent endpoint is started by a Secure Tunnels agent. The endpoint lives for the lifetime of the process and forwards traffic to a port or URL of your choosing.
Create the endpoint https://example.ngrok.app
and forward its traffic to a
local port.
- Agent CLI
- Agent Config
- SSH
- Go
- Javascript
- Python
- Rust
- Kubernetes Controller
ngrok http 8080 --url https://example.ngrok.app
version: 3
endpoints:
- name: example
url: https://example.ngrok.app
upstream:
url: 8080
ssh -R example.ngrok.app:443:localhost:8080 v2@connect.ngrok-agent.com http
import (
"context"
"net"
"golang.ngrok.com/ngrok"
"golang.ngrok.com/ngrok/config"
)
func ngrokListener(ctx context.Context) (net.Listener, error) {
upstreamURL, _ := url.Parse("http://localhost:8080")
return ngrok.ListenAndForward(ctx,
upstreamURL,
config.HTTPEndpoint(
config.WithURL("https://example.ngrok.app"),
),
ngrok.WithAuthtokenFromEnv(),
)
}
Go Package Docs:
const ngrok = require("@ngrok/ngrok");
(async function () {
const listener = await ngrok.forward({
addr: 8080,
authtoken_from_env: true,
domain: "example.ngrok.app",
});
console.log(`Ingress established at: ${listener.url()}`);
})();
Javascript SDK Docs:
import ngrok
listener = ngrok.forward("localhost:8080", authtoken_from_env=True,
domain="example.ngrok.app")
print(f"Ingress established at: {listener.url()}");
Python SDK Docs:
use ngrok::prelude::*;
async fn listen_ngrok() -> anyhow::Result<impl Tunnel> {
let sess = ngrok::Session::builder()
.authtoken_from_env()
.connect()
.await?;
let tun = sess
.http_endpoint()
.domain("example.ngrok.app")
.listen()
.await?;
println!("Listening on URL: {:?}", tun.url());
Ok(tun)
}
Rust Crate Docs:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
k8s.ngrok.com/modules: ngrok-module-set
spec:
ingressClassName: ngrok
rules:
- host: example.ngrok.app
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
Cloud Endpoint
Cloud endpoints are created via the ngrok Dashboard or API. They are persistent and live until they are deleted. Traffic Policy controls how a cloud endpoint handles traffic.
Create an HTTPS cloud endpoint
which returns a Hello world!
200 OK response.
- Agent CLI
ngrok api endpoints create \
--url https://example.ngrok.app \
--traffic-policy "$(<traffic-policy.yml)"
traffic-policy.yml
on_http_request:
- actions:
- type: custom-response
config:
status_code: 200
headers:
content-type: text/plain
content: "Hello world!"
URL
Public
HTTP
- The hostname must be a domain with a valid public suffix.
- The port must be
80
. If you do not specify a port, the default80
will be used for you.
Examples
http://example.ngrok.app
http://example.ngrok.app:80
http://example.party
http://example.ngrok.app:81
- invalid port: port number must be80
, not81
http://example.doesnotexist
- invalid hostname:.doesnotexist
is not a public suffix domainhttp://example.internal
- invalid hostname:.internal
is not a public suffix domain
HTTPS
- The hostname must be a domain with a valid public suffix.
- The port must be
443
. If you do not specify a port, the default443
will be used for you.
Internal
Kubernetes
Valid URLs
Invalid URLs
Validation
When you create an agent endpoint, if you do not specify a complete URL, following defaults are used to construct an endpoint URL. When you create a cloud endpoint, you must always specify both a scheme and hostname.
URL Part | Default |
---|---|
Scheme | https |
Hostname | randomly selected |
Port | 443 if scheme is https 80 if scheme is http |
Consult the following table of examples of URL defaulting:
Value | Endpoint URL |
---|---|
https://example.ngrok.app | https://example.ngrok.app |
http://example.ngrok.app | http://example.ngrok.app |
example.ngrok.app | https://example.ngrok.app |
app.example.com | https://app.example.com |
https://example.internal | https://example.internal |
https://example.internal:1234 | https://example.internal:1234 |
http://example.internal | http://example.internal |
foo.internal | https://foo.internal |
{empty} | https://1eb2-181-80-12-3.ngrok.app (randomly selected) |
If you would like to listen for both http and https traffic, create two endpoints.
Domains
When you create a public endpoint, it must match a Domain on your account. Domains help you set up branded domains and manage TLS certificates. You may create endpoints with wildcard domains as well.
Endpoints with randomly-assigned hostnames are an exception and won't match an existing Domain object.