> ## Documentation Index
> Fetch the complete documentation index at: https://instabox.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Use your own domain

> Serve a published port on your own domain by running a reverse proxy in front of its URL.

A published port has a URL on the InstaBox domain, such as `https://3000-<id>.instabox.tech`. To
serve the same app at `https://app.example.com`, run a reverse proxy on your domain and let it pass
requests to the port's URL. Use this when a customer or a teammate should see your name in the
address bar, or when a webhook sender needs a fixed address that you control.

InstaBox does not host your domain or its certificate. You own the DNS record, the certificate, and
the proxy.

```mermaid theme={null}
flowchart LR
  A["Browser"] -->|"app.example.com"| B["Your reverse proxy"]
  B -->|"3000-<id>.instabox.tech"| C["InstaBox proxy"]
  C -->|"port 3000"| D["Your server in the machine"]
```

## Before you start

* Publish the port and copy its URL. See [Public and private ports](https://instabox.sh/docs/access/ports.md).
* Point a DNS record for your domain at the host that runs your proxy.
* Have a certificate for your domain on that host. Caddy gets one by itself when the DNS record
  points at it.

## Proxy a public port

The InstaBox proxy finds the port by the `Host` header, so your proxy must send the port's own host
name as `Host`, not your domain. It must also connect over HTTPS on port 443, because the port's URL
does not answer plain HTTP.

```text Caddyfile theme={null}
app.example.com {
	reverse_proxy https://3000-<id>.instabox.tech {
		header_up Host {upstream_hostport}
	}
}
```

```nginx nginx theme={null}
map $http_upgrade $connection_upgrade {
	default upgrade;
	''      close;
}

server {
	listen 443 ssl;
	server_name app.example.com;
	ssl_certificate     /etc/ssl/app.example.com.pem;
	ssl_certificate_key /etc/ssl/app.example.com.key;

	location / {
		proxy_pass https://3000-<id>.instabox.tech;
		proxy_set_header Host 3000-<id>.instabox.tech;
		proxy_ssl_server_name on;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;
	}
}
```

Open `https://app.example.com`. You see the same response as on the port's own URL.

> [!WARNING]
>
> A public port stays reachable at its `instabox.tech` URL too. Access rules on your proxy, such as
> a login or an IP allow list, do not protect that URL. To make your proxy the only way in, use a
> private port.

## Proxy a private port

A private port answers only requests that carry its token. Publish the port with `--private`, take
the token from the link it prints (the part after `?_token=`), and let your proxy send it in an
`Authorization` header. Your visitors then need no link, and whoever reaches the port's own URL
without the token gets `401`.

Keep the token out of the config file. This Caddyfile reads it from the environment variable
`INSTABOX_PORT_TOKEN`.

```text Caddyfile theme={null}
app.example.com {
	reverse_proxy https://3000-<id>.instabox.tech {
		header_up Host {upstream_hostport}
		header_up Authorization "Bearer {env.INSTABOX_PORT_TOKEN}"
	}
}
```

In nginx, add this line next to the other `proxy_set_header` lines.

```nginx theme={null}
proxy_set_header Authorization "Bearer ibp_...";
```

The InstaBox proxy removes the token before it passes the request on, so your app does not see it.

> [!WARNING]
>
> Your proxy now holds the port's token, and everyone who can reach your proxy can reach the app.
> Add your own sign-in on the proxy if the app needs one. When the token leaks, rotate it and
> give the proxy the new one. See [Replace a private link](https://instabox.sh/docs/access/ports.md#replace-a-private-link).

## WebSockets

Caddy passes a WebSocket upgrade to the port without extra config. In nginx, the `Upgrade` and
`Connection` lines in the config above do it. The `Authorization` header that the proxy adds
applies to the upgrade request too, so a WebSocket to a private port needs nothing more.

nginx closes a WebSocket when the app sends nothing for 60 seconds. If your connections stay
silent for longer, raise the limit with `proxy_read_timeout 1h;` in the `location` block, or let
the app send a ping more often. Caddy does not close a silent WebSocket after 60 seconds.

## Limits

Your app is two proxies away from the visitor, and the InstaBox proxy does not know your domain.

* **Host headers.** Your app sees `Host: 127.0.0.1:<port>`, and `X-Forwarded-Host` is the port's
  `instabox.tech` name, not your domain. An `X-Forwarded-Host` that your proxy sends is replaced.
  If the app needs your domain, send it in a header of your own and read that header in the app:
  `header_up X-Original-Host {host}` in Caddy, `proxy_set_header X-Original-Host $host;` in nginx.
* **The visitor's address.** `X-Forwarded-For` holds the address of your proxy, not of the
  visitor, and an `X-Forwarded-For` that your proxy sends is replaced. To give the app the
  visitor's address, send it in a header of your own: `header_up X-Real-IP {remote_host}` in
  Caddy, `proxy_set_header X-Real-IP $remote_addr;` in nginx.
* **Redirects.** A relative redirect, such as `Location: /login`, works. An absolute redirect that
  the app builds from the host it sees, or a redirect to `localhost:<port>`, reaches your proxy
  with the port's `instabox.tech` URL in it. nginx with the config above changes that URL back to
  your domain. Caddy passes it on, so the visitor leaves your domain. To change it in Caddy, add
  the line below inside `reverse_proxy`, or set the app's public URL to your domain in its config.

  ```text Caddyfile theme={null}
  header_down Location "^https://3000-<id>\.instabox\.tech" "https://app.example.com"
  ```
* **Cookies.** The InstaBox proxy removes the `Domain` attribute of every cookie the app sets and
  adds `Secure`. The browser therefore keeps the cookie for exactly `app.example.com`, not for its
  subdomains, and only over HTTPS.
* **The visitor's `Authorization` header.** The proxy's `Authorization` line replaces a header
  that the visitor sends, so the app never sees the visitor's own. Do not add the token as a
  second `Authorization` field: a request with two is refused with `400`. If your app reads
  `Authorization` itself, send the token in the cookie `__Host-instabox_port_auth` instead. The
  InstaBox proxy removes that cookie and passes the visitor's `Authorization` header and other
  cookies to the app. In Caddy, write this line in place of the `Authorization` line.

  ```text Caddyfile theme={null}
  header_up +Cookie "__Host-instabox_port_auth={env.INSTABOX_PORT_TOKEN}"
  ```

  In nginx, write this line in place of the `Authorization` line.

  ```nginx theme={null}
  proxy_set_header Cookie "__Host-instabox_port_auth=ibp_...; $http_cookie";
  ```
* **Streaming in nginx.** nginx holds a response back until its buffer fills or the response
  ends, so server-sent events arrive late and together. Add `proxy_buffering off;` to the
  `location` block to pass each event on when the app sends it. Caddy needs no setting.
* **One port per route.** A route in your proxy points at one published port. The port's URL does
  not change, also when you unpublish the port and publish it again, but another port or another
  machine has another URL. A private port that you publish again gets a new token.

## Troubleshooting

> [accordion] 404 with a JSON body: route not found
>
> The request reached InstaBox with your domain as `Host`. Send the port's host name: `header_up
>         Host {upstream_hostport}` in Caddy, `proxy_set_header Host 3000-<id>.instabox.tech` in nginx.

> [accordion] 404: This URL is not published.
>
> The `Host` header is right, but no port is published under that name. Compare the host name in
> your proxy config with the output of `instabox port ls`, and publish the port again if you
> unpublished it.

> [accordion] The proxy cannot connect, or reports a certificate error
>
> Connect to the port's host name over HTTPS on port 443. Plain HTTP on port 80 does not answer.
> The certificate is for `*.instabox.tech`, so a proxy that checks the certificate must check it
> against the port's host name and not against your domain.

> [accordion] 401: This URL is private.
>
> The proxy did not send the port's token, or sent an old one. Check that the `Authorization`
> header is `Bearer` followed by the current token. After you rotate the token, update the proxy.

> [accordion] 502 or 503
>
> `502`: nothing listens on the port in the machine. Start your server. `503`: the machine is
> paused by you, stopped, or still waking. A machine that paused itself wakes on the first
> request. See [Reach a paused machine](https://instabox.sh/docs/access/ports.md#reach-a-paused-machine).

> [card] [Public and private ports](https://instabox.sh/docs/access/ports.md)
>
> Publish a port, make it private, and rotate its token.

> [card] [Auto-pause and max-run](https://instabox.sh/docs/machines/auto-pause.md)
>
> When a machine pauses itself and what wakes it.
