# Domains and HTTPS

Give a Hangar service a public address, on a generated domain or your own, with a TLS certificate issued and renewed for you.

A service is private until you give it a domain. Domains are managed in the service's **Settings → Networking**.

## Generated domain

Use the generate button next to the host field to get a regional domain for the service. It works right away and is the fastest way to see a new deploy in the browser.

## Your own domain

1. Add a domain and enter the hostname, for example `api.example.com`.
2. In your DNS provider, create the record the dashboard shows for that hostname, pointing at the region your service runs in.
3. Turn **HTTPS** on and choose **Let's Encrypt** as the certificate provider.

Once the DNS record resolves, the certificate is issued automatically and renewed before it expires. There is nothing to upload and no renewal to remember.

## Container port

Each domain forwards to one port of your service. Set **container port** to the port your app listens on inside the container, for example `3000` for most Node.js apps or `8000` for a Python app started with gunicorn.

Hangar sets the `PORT` variable to the container port of the service's first domain, `3000` when it has none, unless you set `PORT` yourself. An app that listens on `PORT` needs no port setting; changing the container port moves the app with it on the next deploy.

Your app must listen on `0.0.0.0`, not on `127.0.0.1`, or the request cannot reach it.

## Paths

A domain can route a single path to a service. With a path of `/api`, only requests under `/api` reach it, which lets two services share one hostname. Turn on **strip path** if the service expects requests without that prefix.

## Several domains

A service can have as many domains as you need, each with its own port and path. Apex and `www` are two domains; add both and redirect one to the other from the service's redirect settings.

## Troubleshooting

- **Certificate not issued**: the DNS record does not point at Hangar yet. Check it with `dig` and wait for it to propagate.
- **502 or connection refused**: the container port does not match the port the app listens on, or the app listens on `127.0.0.1`.
