# Run behind Cloudflare or another proxy

Every request reaches your app through Salus’s gateway, which is a proxy in its own right. Many frameworks need to be told that, or they treat each request as plain HTTP and reject form posts. This guide covers what the gateway sends, the setting each framework needs, and how to put Cloudflare or another proxy in front.

## What your app receives

Salus’s gateway handles HTTPS and forwards each request to your app over plain HTTP, on the [port](https://salus.cloud/docs/deployments/environment-configurations/configuring/#port) in its configuration. These headers tell your app what the visitor actually requested:

| Header | Value |
| --- | --- |
| `Host` | The hostname the request was sent to: your custom domain or your app’s Salus address. |
| `X-Forwarded-Proto` | `https`. |
| `X-Forwarded-For` | Ends with the address the gateway received the request from: the visitor, or your proxy if you run one in front. Earlier entries were sent by the client or a proxy, so don’t trust them unless a proxy you control added them. |
| `X-Forwarded-Host` | Not set by Salus. If a proxy in front of Salus sets it, it reaches your app unchanged. |

Any other forwarding header in the request, such as `CF-Connecting-IP`, `X-Real-IP` or `X-Forwarded-Port`, also reaches your app unchanged, whoever sent it. Trust one only once requests can come only from your proxy (see below). Plain `http://` requests never reach your app: Salus redirects them to `https://`.

So the connection your app sees is `http`, while the visitor used `https`. A framework that checks where form posts come from compares the browser’s `https://` origin with the request, and rejects the post unless it trusts `X-Forwarded-Proto`.

## Framework settings

With nothing in front of Salus, there’s one proxy to trust: the gateway.

**Astro** (server output with the Node adapter). Astro ignores `X-Forwarded-Proto` and `X-Forwarded-Host` unless the domain is listed in `security.allowedDomains`, so `checkOrigin` rejects form posts with “Cross-site POST form submissions are forbidden”. List every hostname your app is served on:

```js
// astro.config.mjs
export default defineConfig({
  security: {
    checkOrigin: true,
    allowedDomains: [
      { hostname: 'www.example.com', protocol: 'https' },
      // and your app's Salus address, from its deployment
      { hostname: 'your-app.salus.sh', protocol: 'https' },
    ],
  },
});
```

Once a domain is allowed, Astro reads `clientAddress` from the first `X-Forwarded-For` entry, which a client can set. Don’t rely on it alone, for example to rate-limit sign-ups.

**Next.js.** Server Actions work behind Salus without a setting. Behind a proxy that forwards to your Salus address, have the proxy set `X-Forwarded-Host`, or list your domain in `experimental.serverActions.allowedOrigins`.

**Express.**

```js
app.set('trust proxy', 1);
```

`req.protocol` becomes `https` and `req.ip` the visitor’s address.

**Django.** Add your domain and your app’s Salus address to `ALLOWED_HOSTS`, and:

```python
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
```

Behind a proxy that forwards to your Salus address, also set `USE_X_FORWARDED_HOST = True` if the proxy sends `X-Forwarded-Host`, or list your domain in `CSRF_TRUSTED_ORIGINS`.

**Flask.**

```python
from werkzeug.middleware.proxy_fix import ProxyFix

app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1)
```

Add `x_host=1` if a proxy you run sends `X-Forwarded-Host`.

**FastAPI with Uvicorn.** Uvicorn trusts forwarded headers only from `127.0.0.1` by default, and the gateway isn’t local:

```bash
uvicorn main:app --host 0.0.0.0 --port 8080 --proxy-headers --forwarded-allow-ips='*'
```

## Put Cloudflare in front

**DNS only.** Add your [custom domain](https://salus.cloud/docs/deployments/environment-configurations/configuring/#domains) in Salus and point its record at Salus with Cloudflare’s proxy off (**DNS only**). Salus verifies the record and issues the certificate, and requests go straight to Salus.

**Proxied**, for Cloudflare’s firewall, caching or rate limiting. Before Salus serves a custom domain and issues its certificate, it checks that the domain’s record points at Salus. A proxied record answers with Cloudflare’s addresses, so it doesn’t pass that check. Instead, keep the record proxied and forward requests to your app’s Salus address with a Worker on your domain’s route, or any proxy that can send requests on to a different hostname:

```js
// Cloudflare Worker. SALUS_ORIGIN is https:// plus your app's Salus address.
export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const target = new URL(url.pathname + url.search, env.SALUS_ORIGIN);
    const forwarded = new Request(target, request);
    forwarded.headers.set('X-Forwarded-Host', url.host);
    forwarded.headers.set('X-Visitor-IP', request.headers.get('CF-Connecting-IP') ?? '');
    forwarded.headers.set('X-Origin-Secret', env.ORIGIN_SECRET);
    return fetch(forwarded);
  },
};
```

Your app then receives your Salus address as `Host` and your domain as `X-Forwarded-Host`, so allow your domain in the framework settings above. The visitor’s address arrives in `X-Visitor-IP`; `X-Origin-Secret` is explained next.

## Only accept requests from your proxy

Salus can’t yet limit an app to your proxy’s addresses, and your app’s Salus address stays reachable. Anyone who finds it can skip your proxy and its firewall. Close that gap in the app: have the proxy send a secret header, and reject requests without it.

1.  Generate a long random value. Store it as a Worker secret named `ORIGIN_SECRET`, and as an [environment variable](https://salus.cloud/docs/deployments/environment-configurations/variables/) with the same name in Salus.
2.  In the app, reject any request that doesn’t carry it. Make an exception for your [health-check](https://salus.cloud/docs/deployments/environment-configurations/configuring/#health-checks) path, which Salus calls directly:

```js
// Express
app.use((req, res, next) => {
  if (req.path === '/healthz') return next();
  if (req.get('x-origin-secret') !== process.env.ORIGIN_SECRET) return res.sendStatus(403);
  next();
});
```

Once only your proxy can reach the app, you can trust the headers it sets, such as `X-Visitor-IP`.
