---
url: /runcobo/guide/proxies.md
description: >-
  Get the client IP, scheme and host — correctly, behind load balancers and
  reverse proxies.
---

# Proxies & Client Info

Inside an action, `request` exposes the effective connection details:

| Method | Example | Description |
| --- | --- | --- |
| `request.remote_ip` | `"203.0.113.7"` | The client's IP address |
| `request.scheme` | `"https"` | `http` or `https` |
| `request.ssl?` | `true` | Whether the scheme is `https` |
| `request.host` | `"api.example.com"` | Host name, without the port |
| `request.port` | `443` | Port |
| `request.protocol` | `"https://"` | Scheme with `://` |
| `request.base_url` | `"https://api.example.com"` | Scheme, host and port — handy for building absolute URLs |

## Behind a proxy

Without configuration, Runcobo **trusts no proxy**. Values come from the TCP connection and the `Host` header, and every `X-Forwarded-*` header is ignored — otherwise any client could claim any IP by sending a header.

When your app runs behind a load balancer, list the proxies you trust:

```crystal [src/main.cr]
Runcobo.start(
  proxy_config: Runcobo::ProxyConfig.new(
    trusted_proxies: ["10.0.0.0/8", "127.0.0.1", "::1"],
  ),
)
```

Entries are IPv4 or IPv6 addresses, or CIDR ranges. Invalid entries raise at startup.

When a request comes from a trusted address, Runcobo uses:

| Header | Used for |
| --- | --- |
| `X-Forwarded-For` | `remote_ip`: walks the list from the right, skipping trusted proxies, and stops at the first untrusted address |
| `X-Forwarded-Proto` | `scheme` (`http` or `https` only) |
| `X-Forwarded-Host` | `host` and port |
| `X-Forwarded-Port` | `port` |

When a header contains several comma-separated values, the last one (added by the nearest proxy) is used. Malformed values are ignored rather than trusted.

### Single-address headers

Some providers put the client IP in a header of their own, such as `CF-Connecting-IP` or `Fly-Client-IP`. Tell Runcobo to read it — still only from trusted proxies:

```crystal
Runcobo::ProxyConfig.new(
  trusted_proxies: ["10.0.0.0/8"],
  client_ip_header: "Fly-Client-IP",
)
```

The configuration belongs to the server it's passed to; nothing is set globally.

::: warning Only trust what's in front of you
List only addresses that your own infrastructure controls. Trusting `0.0.0.0/0` lets any client spoof its IP address and scheme.
:::
