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

# Gateway Port Policy

> Control which CVM guest ports the gateway forwards and which receive PROXY protocol headers

A gateway port policy controls which ports on your CVM receive traffic from the gateway, and which of those ports get a PROXY protocol header.

Restrictive policies and PROXY protocol require **dstack OS 0.5.9 or later**. Phala Cloud rejects them on older images; upgrade the OS image first.

## Guest ports

The policy matches **guest ports**, which are the left side of a Docker Compose port mapping. For `ports: ["8080:80"]`, Nginx listens on container port `80` and Docker publishes it on guest port `8080`, so the policy lists `8080`.

* Ports bound only to loopback (such as `127.0.0.1:8080:80`) cannot be reached from the gateway regardless of policy.
* The policy does not publish container ports, does not affect host-mapped ports, and has no effect when the gateway is disabled for the CVM.

## Policy modes

A policy consists of `restrict_mode` and a list of ports.

| Configuration | Gateway behavior |
| - | - |
| `restrict_mode` on, ports listed | Forwards only the listed ports. |
| `restrict_mode` on, empty list | Blocks all ports. Requires explicit confirmation. |
| `restrict_mode` off | Forwards all ports. The list only selects which ports receive a PROXY protocol header. |
| No policy declared | Forwards all ports without PROXY protocol. |

> [!NOTE]
>
> New CVMs enable `restrict_mode` by default and automatically populate the port policy from your Docker Compose `ports` mappings. GPU rental orders are the exception: they forward all ports unless you set a policy. If the generated policy is inaccurate or misses a port, [update it manually](#configure-a-policy).

## Default policy for new CVMs

When you create a CVM without declaring a policy, Phala Cloud turns on `restrict_mode` and allows:

* every static TCP guest port published in Docker Compose,
* port `8090` (guest agent),
* port `22` when SSH is enabled (dev OS image, SSH keys, or SSH dev box).

Phala Cloud skips the default policy (forwarding all ports) when the OS image is older than dstack OS 0.5.9, the CVM uses the Bash runner, or a service uses `host` or another non-default network mode. GPU rental orders also skip it; the launch form's **Gateway port policy** field forwards all ports by default.

Ports set via environment variables (such as `"${PORT}:80"`) cannot be resolved statically, so they are left out of the generated allowlist and stay closed until you add them manually.

## Special ports: 8090 and 22

### Guest agent (port 8090)

Phala Cloud reads CVM logs and attestation data through the guest agent on port `8090`. Access to port `8090` is controlled by **two layers**:

1. **Gateway port policy (this page):** Controls whether the gateway forwards port `8090` at all. Omitting `8090` from a restricted policy blocks everyone, including Phala Cloud, and requires explicit confirmation in the console, CLI, and API.
2. **[Guest Agent Access](https://docs.phala.com/phala-cloud/networking/guest-agent-access.md):** Controls who can reach port `8090` while the port policy allows it. By default, only Phala Cloud can reach it; you can open it publicly when third parties need to verify attestation.

In the console, choose who can reach the guest agent under the app's **Networking** → **Dstack Guest Agent** → **Access**:

| Console choice | Port policy (`8090`) | Effect |
| - | - | - |
| **Anyone** | Allowed | Anyone who can reach the gateway can reach the guest agent. |
| **Phala Cloud** (default) | Allowed | Public traffic is blocked; logs and attestation keep working. |
| **No one** | Blocked | Nobody, including Phala Cloud, can reach the guest agent. Logs and attestation stop working. |

Choosing **No one** removes `8090` from the port policy and restarts the app's instances after you confirm. The **Port policy** card always keeps `8090`, so close it here instead. With the CLI or API, leave `8090` out of a restricted policy and confirm the closure (see below).

To block public access without breaking logs or attestation, choose **Phala Cloud**. See [Guest Agent Access](https://docs.phala.com/phala-cloud/networking/guest-agent-access.md).

### SSH (port 22)

Omitting port `22` from a restricted policy blocks SSH through the gateway. Keep `22` in the policy if you connect over SSH; see [Enable SSH Access](https://docs.phala.com/phala-cloud/networking/enable-ssh-access.md).

## PROXY protocol

Enable PROXY protocol on a port only when the service listening on it expects a PROXY header before application data. Listeners expecting plain HTTP, TLS, or SSH reject connections that start with a PROXY header (HTTP servers typically reply with `400`).

* The gateway writes PROXY protocol v2 headers, or passes through an upstream load balancer's header (which may be v1). Configure listeners to accept both versions.
* Update the listener and the port policy in the same deployment so both sides agree.

## Restarts and multi-instance apps

* **Restarts:** Effective policy changes restart a running CVM; a stopped CVM picks up the new policy on its next start. No-op updates (same forwarded ports and PROXY settings) do not restart the CVM.
* **On-chain KMS:** Register the new compose hash on-chain before applying the change. See [Updating with Onchain KMS](https://docs.phala.com/phala-cloud/key-management/updating-with-onchain-kms.md).
* **Multiple instances:** Policies are per-instance. For any given port, the gateway only routes traffic to instances whose policy allows it, so keep policies in sync across instances.

## Configure a policy

### Console

Open the app, go to **Networking** → **Access control**, and edit the **Port policy** card: toggle **Restrict mode**, click **Add port** to add guest ports, and use the **Proxy protocol (pp)** switch on each row as needed. Saving applies the policy to every instance of the app and restarts the running ones after you confirm.

On the GPU rental launch form, the **Gateway port policy** field defaults to forwarding all ports and asks for confirmation before closing port `8090`.

### phala.toml

Add a `[port_policy]` table to `phala.toml` and run `phala deploy`:

```toml theme={"system"}
[port_policy]
restrict_mode = true
ports = [
  { port = 8090 },
  { port = 8080 },
  { port = 8443, pp = true },
]
```

Both `restrict_mode` and `ports` are required. Each entry takes a guest `port` and an optional `pp = true` to turn on PROXY protocol. To block every port, set `restrict_mode = true` with `ports = []` and apply with `phala deploy --confirm-close all`.

`phala deploy` replaces the CVM's entire policy rather than merging. If you omit `[port_policy]`, existing CVMs keep their current policy and new CVMs get the default policy. See the [`phala.toml` reference](https://docs.phala.com/phala-cloud/phala-cloud-cli/phala-toml) for other keys.

### CLI

Use `phala port-policy` to inspect or change a single CVM's policy. Pass `--cvm-id <id>`, or omit it to target the CVM linked in `phala.toml`.

```bash theme={"system"}
# Inspect the current policy
phala port-policy show

# Allow a port (keeps existing PROXY setting if already listed)
phala port-policy allow 8081
phala port-policy allow 8081 --proxy-protocol
phala port-policy allow 8081 --no-proxy-protocol

# Remove a port from a restricted policy
phala port-policy deny 8081

# Replace the entire policy
phala port-policy replace --restrict --port 8090 --port 8080 --proxy-protocol-port 8443
phala port-policy replace --no-restrict
phala port-policy replace --deny-all --confirm-close all --yes
```

* **Command constraints:** When a policy already forwards all ports, `allow` requires `--proxy-protocol` or `--no-proxy-protocol`, and `deny` cannot block individual ports (use `replace --restrict` instead). Passing `replace --restrict` with no ports is rejected; use `--deny-all`.
* **Scripting and waiting:** Changes display a before/after summary and prompt for confirmation. Pass `--yes` in non-interactive scripts and `--wait` to wait until the update finishes.
* **Closing confirmations:** Newly blocking port `8090`, newly blocking port `22` on an existing CVM, or blocking every port requires a separate confirmation, interactively or via `--confirm-close 8090`, `--confirm-close 22`, or `--confirm-close all` (which covers `8090` and `22`, and is required when blocking all ports). `--yes` does not satisfy this check. Both `phala port-policy` and `phala deploy` validate this locally before sending a request.
* **Unlisted port warnings:** Both `phala port-policy` and `phala deploy` warn when a restricted policy omits a port published in Docker Compose (env-var ports may not be detected). Ignore the warning if intentional.
* **Sync with `phala.toml`:** `phala port-policy` updates only the CVM, not `phala.toml`. Copy manual changes into `[port_policy]` so the next `phala deploy` does not overwrite them.
* **Concurrent edits:** The CLI sends the compose hash it read and rejects stale updates. Re-run `phala port-policy show` and retry if someone else modified the CVM.

### API

Send `compose.port_policy` in `PATCH /cvms/{cvm_id}`:

```json theme={"system"}
{
  "compose": {
    "port_policy": {
      "restrict_mode": true,
      "ports": [{ "port": 8090 }, { "port": 8080 }, { "port": 8443, "pp": true }]
    }
  }
}
```

* **Merge semantics:** Fields inside `port_policy` merge by key. Sending `ports` replaces the list; omitting `ports` keeps the existing list.
* **Concurrency:** Send top-level `expected_compose_hash` with the compose hash you read to reject concurrent changes with `409`.
* **Closing port `8090`:** Omit `8090` from a restricted policy and include top-level `"guest_agent_gateway_access": "closed"` in the same request. Without it, Phala Cloud adds `8090` back automatically. Sending this field while `8090` is still allowed is rejected.
* **Unlisted ports:** A `202` response includes `port_policy_unlisted_ports`, listing published TCP ports omitted by a restricted policy (env-var ports may be absent).

See [Update a CVM](https://docs.phala.com/phala-cloud/update/update-cvm-configuration.md) for full request details.

## Troubleshooting

If a port is unreachable through the gateway, check these in order:

1. **Docker Compose mapping:** Confirm the port is published (not bound to `127.0.0.1`) and the policy lists the **guest port** (left side).
2. **Allowlist:** With `restrict_mode` on, confirm the port is listed, especially if it comes from an environment variable like `"${PORT}:80"`.
3. **PROXY protocol mismatch:** Confirm PROXY protocol is enabled only when the listener expects a PROXY header, and disabled when it expects plain traffic.
4. **Older OS image:** On images older than dstack OS 0.5.9, the gateway forwards all ports, so the cause is elsewhere. See [Networking Troubleshooting](https://docs.phala.com/phala-cloud/networking/troubleshooting.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.