# Remote Deployment

Run GitLab MCP Server for other people: as an OS service, in Docker, behind nginx, Caddy, Traefik, Apache or a Cloudflare Tunnel, and across several instances with token affinity.

Source: https://docs.jmrp.io/gitlab-mcp-server/operations/remote-deployment/
Updated: 2026-11-24

---

> **At a glance**
>
> - **In HTTP mode the server holds no credential of its own.** Every request carries the caller's token, so there is no server token to hide in a unit file, `/health` needs no authentication, and running several instances is a caching problem rather than a state-replication one.
> - **Nothing here needs a reverse proxy to be correct.** The binary terminates TLS, reloads a renewed certificate on its own, answers its own CORS preflights, and can listen on a unix socket.
> - **Several instances cannot be balanced round-robin for free.** The server pool and the OAuth identity cache are per process, so a caller that moves has its entry built and its token verified again on each instance. Pin it with token affinity, which stateful sessions and subscriptions require.

[HTTP Server Mode](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/) documents the
transport flag by flag. This page is the other half: how to stand one up for
other people, on a machine that stays running, reachable from somewhere other
than the operator's laptop.

Everything here assumes HTTP mode. There is no remote form of stdio: stdio is a
pipe between one client process and one server process on the same machine, and
the moment a second person needs to reach it the answer is HTTP.

## Pick a shape first

| Situation | Shape |
| --- | --- |
| One host, proxy and server together | Unix socket: `--http-addr=/run/gitlab-mcp/server.sock` with `--trusted-proxy-header=X-Real-IP --trusted-proxies=unix` |
| Proxy on another host | `--tls-cert` and `--tls-key` on the listener |
| No proxy at all | `--tls-cert` and `--tls-key` plus `--auth-mode=oauth`; read [Direct exposure](https://docs.jmrp.io/gitlab-mcp-server/operations/remote-deployment/#direct-exposure-no-proxy) |
| Containers | The published image, read-only root filesystem, digest pin |
| More than one instance | A balancer with token affinity: [Several instances](https://docs.jmrp.io/gitlab-mcp-server/operations/remote-deployment/#several-instances-behind-one-proxy) |

## Run it as a service

- **systemd**

  Two shapes, and what separates them is whether a same-host proxy has to reach a
  unix socket. **Loopback TCP with a dynamic user** is the least privileged form:

  ```ini
  # /etc/systemd/system/gitlab-mcp-server.service
  [Unit]
  Description=GitLab MCP Server
  Documentation=https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/
  After=network-online.target
  Wants=network-online.target

  [Service]
  Type=exec
  DynamicUser=yes
  EnvironmentFile=/etc/gitlab-mcp-server/env
  ExecStart=/usr/local/bin/gitlab-mcp-server \
      --http \
      --http-addr=127.0.0.1:8080 \
      --gitlab-url=https://gitlab.example.com \
      --auth-mode=oauth \
      --public-url=https://mcp.example.com \
      --trusted-proxy-header=X-Real-IP \
      --trusted-proxies=127.0.0.1
  Restart=on-failure
  RestartSec=2s
  StandardInput=null

  NoNewPrivileges=yes
  ProtectSystem=strict
  ProtectHome=yes
  ProtectKernelTunables=yes
  ProtectKernelModules=yes
  ProtectControlGroups=yes
  PrivateTmp=yes
  PrivateDevices=yes
  RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
  RestrictNamespaces=yes
  RestrictSUIDSGID=yes
  LockPersonality=yes
  MemoryDenyWriteExecute=yes
  SystemCallArchitectures=native
  SystemCallFilter=@system-service
  CapabilityBoundingSet=
  AmbientCapabilities=
  MemoryMax=512M

  [Install]
  WantedBy=multi-user.target
  ```

  `ProtectSystem=strict` mounts the whole filesystem read-only and the server is
  fine with that: it writes nothing to disk. Logs go to standard error, which
  `journald` collects, and the only file it ever creates is the unix socket of
  the second shape below. Reads are unaffected, so certificates and the
  environment file stay readable.

  `RestrictAddressFamilies=` deliberately omits `AF_NETLINK`. The released binary
  is built with `CGO_ENABLED=0` and uses Go's own resolver, which reaches DNS over
  UDP and TCP and asks the kernel nothing through netlink. Verified against this
  unit: a credential check that had to resolve and reach `gitlab.com` was answered
  by GitLab with and without `AF_NETLINK` in the list.

  **A unix socket needs a fixed user.** `DynamicUser=yes` allocates a transient
  user and group per start, so there is no group the proxy can be added to.
  Create a system user and share a real group:

  ```bash run
  sudo useradd --system --no-create-home --shell /usr/sbin/nologin gitlab-mcp
  sudo usermod -aG gitlab-mcp www-data   # or nginx, or caddy
  ```

  Then replace the identity and address lines, and the proxy list with it:

  ```ini
  User=gitlab-mcp
  Group=gitlab-mcp
  RuntimeDirectory=gitlab-mcp
  RuntimeDirectoryMode=0750
  ExecStart=/usr/local/bin/gitlab-mcp-server \
      --http \
      --http-addr=/run/gitlab-mcp/server.sock \
      --http-socket-mode=0660 \
      --gitlab-url=https://gitlab.example.com \
      --trusted-proxy-header=X-Real-IP \
      --trusted-proxies=unix
  ```

  A socket's peer has no address, so `unix` is the entry that trusts the proxy on
  it, and each caller then keeps an authentication budget of its own. Keeping
  `--trusted-proxies=127.0.0.1` from the TCP unit does not carry over: a socket
  listener whose list names addresses and no `unix` refuses to start
  ([The unix socket and the authentication budgets](https://docs.jmrp.io/gitlab-mcp-server/enterprise/tls/#the-unix-socket-and-the-authentication-budgets)).

  `RuntimeDirectory=` makes `/run/gitlab-mcp` exist, be owned by the service, and
  be removed on stop. It also has to be **writable**: the server does not bind the
  published path directly. It creates a `0700` staging directory beside it, binds
  and chmods the socket there, then links it into place, so a socket only ever
  appears with its final permissions. A read-only parent directory therefore
  fails, even though the process writes nothing else.

  Keep `/etc/gitlab-mcp-server/env` at mode `0600`, owned by root: systemd reads
  it before dropping privileges, so the service user never needs to. Then install
  and check:

  ```bash
  sudo systemctl daemon-reload
  sudo systemctl enable --now gitlab-mcp-server
  curl -fsS http://127.0.0.1:8080/health
  # The socket shape: curl -fsS --unix-socket /run/gitlab-mcp/server.sock http://localhost/health
  journalctl -u gitlab-mcp-server -f
  ```

- **launchd**

  macOS, as a system daemon under `/Library/LaunchDaemons/`:

  ```xml
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
      <key>Label</key>
      <string>io.github.jmrplens.gitlab-mcp-server</string>
      <key>ProgramArguments</key>
      <array>
          <string>/usr/local/bin/gitlab-mcp-server</string>
          <string>--http</string>
          <string>--http-addr=127.0.0.1:8080</string>
          <string>--gitlab-url=https://gitlab.example.com</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>KeepAlive</key>
      <dict>
          <key>SuccessfulExit</key>
          <false/>
      </dict>
      <key>UserName</key>
      <string>_gitlabmcp</string>
      <key>StandardOutPath</key>
      <string>/usr/local/var/log/gitlab-mcp-server.log</string>
      <key>StandardErrorPath</key>
      <string>/usr/local/var/log/gitlab-mcp-server.log</string>
      <key>EnvironmentVariables</key>
      <dict>
          <key>GITLAB_MCP_LOG_LEVEL</key>
          <string>info</string>
      </dict>
  </dict>
  </plist>
  ```

  ```bash
  sudo launchctl load -w /Library/LaunchDaemons/io.github.jmrplens.gitlab-mcp-server.plist
  sudo launchctl print system/io.github.jmrplens.gitlab-mcp-server
  ```

  `EnvironmentVariables` is fine for a setting such as the log level. A plist is
  world-readable, though, so a secret does not belong there. Put it in a file the
  daemon reads instead, named by `GITLAB_MCP_ENV_FILE` with an absolute path.

- **Windows**

  `sc.exe create` on its own does not work. The Service Control Manager expects
  the executable it starts to register a service control handler and report back,
  and a plain console binary does not, so the start fails with error 1053, "did
  not respond to the start request in a timely fashion". A service wrapper bridges
  that gap:

  ```powershell
  nssm install GitLabMcpServer "C:\Program Files\gitlab-mcp-server\gitlab-mcp-server.exe"
  nssm set GitLabMcpServer AppParameters "--http --http-addr=127.0.0.1:8080 --gitlab-url=https://gitlab.example.com"
  nssm set GitLabMcpServer AppStdout "C:\ProgramData\gitlab-mcp-server\out.log"
  nssm set GitLabMcpServer AppStderr "C:\ProgramData\gitlab-mcp-server\err.log"
  nssm set GitLabMcpServer Start SERVICE_AUTO_START
  nssm start GitLabMcpServer
  ```

  `winget install --id jmrplens.gitlab-mcp-server -e` installs a **user-scope**
  portable package under `%LOCALAPPDATA%\Microsoft\WinGet\Packages\`, reachable
  through a symlink in a per-user `Links` directory. A service does not run as
  that user and must not depend on that path, so add `--scope machine`, which
  installs under `%PROGRAMFILES%\WinGet\Packages\`, or copy the executable to a
  machine-wide directory, and point the wrapper at the real file rather than at
  the symlink.

  `--http-socket-mode` is accepted on Windows and not enforced: the server logs a
  warning and lets the directory ACL decide. Use a TCP listener there.

### Three things every service manager gets wrong

- **State the transport.** `--transport auto` reads file descriptor 0 and picks
  HTTP only when stdin is `/dev/null`. systemd's default `StandardInput=null`
  happens to give the same answer, but a unit that sets `StandardInput=socket`
  or `tty` gets stdio instead. `--http` says what you mean and cannot be moved
  by how the supervisor wires a file descriptor.
- **Socket activation is not supported.** A `.socket` unit passes its socket
  through `LISTEN_FDS`, from file descriptor 3, and launchd's `Sockets` key
  through `launch_activate_socket()`; the server reads neither, so a socket
  passed that way is ignored and the server binds `--http-addr` itself. Only
  `StandardInput=socket`, or launchd's `inetdCompatibility`, puts a socket on
  file descriptor 0, and this server reads a socket there as "a supervisor
  handed me a stdio channel", so under `--transport auto` it would speak
  JSON-RPC down it instead of accepting connections. Bind the socket yourself
  with `--http-addr`, and under systemd with `RuntimeDirectory=`.
- **Secrets belong in an environment file, never on the command line.** A
  service's command line is readable by any local user: a systemd unit's
  `ExecStart=` through `systemctl show` and `/proc`, so a secret goes in
  `EnvironmentFile=`. HTTP mode has no GitLab token to hide, but two real
  secrets exist: `GITLAB_MCP_TELEMETRY_IDENTITY_KEY`, which has no flag for
  exactly this reason, and whatever `OTEL_EXPORTER_OTLP_HEADERS` carries.

A leftover socket is not always cleaned up for you. On start the server probes
an existing socket and removes it only when the connection is refused, which
proves nothing is listening. A live socket is refused rather than stolen, a path
that is not a socket is never deleted, and a probe that fails for any other
reason (a permission denial, for instance) refuses to start and tells you to
remove the file by hand.

## Run it with Docker

The image and its command line are described in
[Docker](https://docs.jmrp.io/gitlab-mcp-server/install/docker/). What follows is the deployment
shape.

### One container

```bash
image=$(curl -fsSL https://raw.githubusercontent.com/jmrplens/gitlab-mcp-server/main/server.json \
  | jq -r '.packages[] | select(.registryType == "oci" and (.identifier | startswith("ghcr.io/"))) | .identifier')
docker run -d --name gitlab-mcp \
  --restart unless-stopped \
  -p 127.0.0.1:8080:8080 \
  --read-only \
  --tmpfs /tmp:rw,size=64m,mode=1777 \
  --cap-drop ALL \
  --security-opt no-new-privileges:true \
  --memory 512m \
  -e GITLAB_URL=https://gitlab.example.com \
  "$image"
```

**The instance arrives as an environment variable, not an argument.** Any
argument after the image name replaces `CMD` wholesale, and `CMD` is
`--transport auto --http-addr 0.0.0.0:8080`. Writing
`docker run <image> --gitlab-url=…` would silently drop both, and with no
transport named the process serves stdio rather than HTTP. `-e GITLAB_URL=`
fills the instance list without touching the command, because HTTP mode reads
the variable when the flag is absent.

**No `-i`.** `auto` serves HTTP precisely when stdin is `/dev/null`, which is
what `docker run` without `-i` gives it. Adding `-i` hands the container a pipe,
which means a client, and the container speaks stdio to nobody.

**`-p 127.0.0.1:8080:8080` binds the published port to loopback.**
`-p 8080:8080` publishes on every interface and, with Docker's default iptables
integration, that goes around the host firewall.

**The plain HTTP line at startup is about the container's own interfaces.**
Inside the container the listener binds `0.0.0.0`, which every container on
the same bridge network can reach, so it warns at startup that tokens cross
that network in the clear, loopback publishing or not. With a proxy in front
that terminates TLS ([Behind a reverse proxy](https://docs.jmrp.io/gitlab-mcp-server/operations/remote-deployment/#behind-a-reverse-proxy)),
`-e GITLAB_MCP_ALLOW_PLAINTEXT_HTTP=true` says so, and the
line is written at `INFO` instead
([Plain HTTP on an address other machines can reach](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#plain-http-on-an-address-other-machines-can-reach)).
In the hardened mode, `-e GITLAB_MCP_ALLOW_PLAINTEXT_HTTP=false`, that
container does not start. The host's network, a sidecar, a unix socket in a
mounted directory or TLS on the listener do, each with what it needs
([The listener rule and the container's network](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#the-listener-rule-and-the-containers-network)).

**`--read-only` works because the image writes nothing at runtime.** The one
exception is a unix socket, whose bind needs a writable parent directory for the
staging step described above, so give it a `--tmpfs` or a volume. The container
already runs as `appuser` (uid 10001), so `--user` is redundant, and
`--cap-drop ALL` is safe because nothing binds a privileged port.

**Pin the digest, not only the tag.** A tag can be re-pushed; a digest is what a
client actually resolves. The first command reads the image the MCP Registry's
`server.json` pins for the current release: the `3.8.0` tag with the
digest the release workflow pushed under it, recorded in the repository when
the release was published rather than read back from the tag now. Docker Hub
holds the same index under the same digest. Resolve it once and keep the value:
a script that reads it again on every start moves to the next release as soon
as one is published.

### Compose

```yaml
services:
  gitlab-mcp-server:
    image: ghcr.io/jmrplens/gitlab-mcp-server:3.8.0
    restart: unless-stopped
    networks: [mcp]
    ports:
      - "127.0.0.1:8080:8080"
    command:
      - "--http"
      - "--http-addr=0.0.0.0:8080"
      - "--gitlab-url=https://gitlab.example.com"
      - "--auth-mode=oauth"
      - "--public-url=https://mcp.example.com"
      - "--trusted-proxy-header=X-Real-IP"
      - "--trusted-proxies=172.28.0.1"
    read_only: true
    tmpfs:
      - /tmp:rw,size=64m,mode=1777
    cap_drop: [ALL]
    security_opt: ["no-new-privileges:true"]
    deploy:
      resources:
        limits: { memory: 512M, cpus: "2.0" }
    healthcheck:
      test: ["CMD", "gitlab-mcp-server", "--probe"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    logging:
      driver: json-file
      options: { max-size: "10m", max-file: "3" }

networks:
  mcp:
    ipam:
      config:
        - subnet: 172.28.0.0/24
```

A `command:` list replaces `CMD`, which is why `--http` and `--http-addr` are
spelled out again. Naming an instance is not optional: HTTP mode exits with
`--gitlab-url is required in HTTP mode` when none is given, because a
deployment that names no instance would send whatever token a caller supplied
to whatever host that caller put in `GITLAB-URL`.

`--trusted-proxies` names where the proxy connects from, and that has to be an
address only the proxy can hold. The example declares its own network with a
fixed subnet and trusts `172.28.0.1` alone: the gateway, which is where a proxy
running on the host arrives from through the published port. A proxy that is
itself a container on that network gets a fixed `ipv4_address`, and that is the
value to trust instead. Docker's default pools (`172.16.0.0/12`) are the wrong
answer: every container on the host lives in them, and any of them could then
name the address a caller's failures are charged to.

Several flags have **no environment-variable equivalent** and must be arguments:
`--http-addr`, `--http-socket-mode`, `--tls-cert`, `--tls-key`,
`--trusted-proxy-header`, `--trusted-proxies`, `--stateless`, `--json-response`,
`--http-idle-timeout`, `--max-request-body-bytes`, `--allow-any-gitlab-url`,
`--resource-documentation`, `--resource-policy-uri` and `--resource-tos-uri`,
and so do the transport selectors `--http` and `--transport` themselves.
Most of the rest can come from the environment, `--rate-limit-rps` and
`--rate-limit-burst` included, and an explicitly passed flag still wins.

The image's own `HEALTHCHECK`, from 3.0.0, is `gitlab-mcp-server --probe`: the
binary finds the server process in the container, reads `--http-addr`,
`--tls-cert` and the transport off that process's command line, and asks
`/health` where it is actually served. Move the listener to another port, to a
unix socket, or behind `--tls-cert`, and the check follows without being told.
A container a client runs over stdio (`docker run -i`) has no listener; the
probe reports it healthy while the process runs. `--probe <url>`,
`--probe unix:<path>` or `--probe host:port` skips the discovery, for a check
run from outside the container.

The Compose block above restates the image's check only so that a change to the
image's interval does not silently change the deployment's; drop the
`healthcheck:` block to use the image's own. An image up to 2.7.5 has no
`--probe`: its own check runs `wget` against `http://localhost:8080/health`, so
any other listener is reported unhealthy while it serves, and the `test:` line
above fails there as an unknown flag. Pinned to one of those, restate the check
with `wget` against wherever that deployment listens.

### Secrets

HTTP mode has no GitLab token to inject. What is left is telemetry credentials,
and those do not belong in `environment:`, which `docker inspect` prints in
full. Mount the file and name it:

```yaml
    secrets:
      - source: mcp_env
        target: /run/secrets/mcp.env
    environment:
      GITLAB_MCP_ENV_FILE: /run/secrets/mcp.env

secrets:
  mcp_env:
    file: ./secrets/mcp.env
```

`GITLAB_MCP_ENV_FILE` is resolved once from the process environment before any
dotenv file is loaded, so a loaded file cannot nominate another one. Give an
absolute path: a relative one is resolved against the working directory and
warned about at startup. It composes with `read_only: true`, because the secret
arrives as a read-only mount.

### One instance or several

`--gitlab-url` may be given more than once, or once comma-separated. One
instance pins the deployment and a `GITLAB-URL` header is ignored. Several
publish an allow-list: the header becomes required, and one naming anything
outside the list is refused rather than honored. `--allow-any-gitlab-url`
publishes none and lets the header name any host, and a request that then omits
it is refused with `400`; the server refuses to start with it unless
`--http-addr` binds a loopback address or a unix socket, so it belongs on a
single-user local deployment only. The full table is in
[Publishing more than one instance](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#publishing-more-than-one-instance).

## Behind a reverse proxy

### What every proxy has to get right

| Requirement | Why |
| --- | --- |
| Declare the host with `--public-url` | The proxy forwards the client's `Host`, and a host nobody declared is refused with `403` as a DNS-rebinding attempt. `--public-url` is what declares it. Listing the proxy's own address in `--trusted-proxies`, together with the `--trusted-proxy-header` it requires, is the alternative: a hop the operator vouched for may forward any host, which is what a deployment fronting several names does. OAuth mode still requires `--public-url` |
| No response buffering | Streamable HTTP answers with `text/event-stream`. A proxy that holds the body, buffering it or compressing it, turns a live stream into one delivery at the end |
| Long read timeout | A `subscriptions/listen` stream is silent between notifications. The server's SSE keep-alive fires every 25 seconds, under nginx's 60-second default, but a quiet stream still wants headroom. A tool call that writes nothing until its result gets no keep-alive, so one silent for longer than the read timeout is cut too: set it above the longest call you expect |
| HTTP/1.1 upstream | HTTP/1.0 to the upstream has no chunked transfer, which is what an SSE body uses |
| Forward `Authorization` | OAuth mode reads the bearer token from it; stripping it turns every request into a `401` |
| Forward `PRIVATE-TOKEN` | Legacy mode's header |
| Forward `GITLAB-URL` | Selects the instance when several are published |
| Real client address | `--trusted-proxy-header` names the header the proxy sets and `--trusted-proxies` the addresses it connects from, or `unix` on a socket listener, so the authentication-failure limiter and the discovery documents' bucket count callers rather than the proxy |
| Route `/.well-known/` unchanged | In OAuth mode the RFC 9728 metadata lives at the **host root**, not under the path prefix |
| Add no CORS headers | The server answers preflight itself. Two `Access-Control-Allow-Origin` headers is a CORS failure, not a merge |

**`--public-url` is what makes the forwarded host acceptable.** Every proxy
below preserves the client's `Host` and connects to the server over loopback,
which is also the shape of a DNS-rebinding attack: a page resolves a name the
attacker controls to the loopback address, and the browser reaches the local
server with that name in `Host`. So a deployment reached as `mcp.example.com`
starts with `--public-url=https://mcp.example.com`, and any other host is still
refused with `403`, on `/health` as well as on `/mcp`. A request that carries no
`Host` at all, which is what a balancer health check sends, is served. The full
rule, the trusted-proxy alternative and what a wildcard listener does are in
[Behind a reverse proxy](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#behind-a-reverse-proxy).

**Name the real client address, or the discovery documents share one bucket.**
The two server cards and the RFC 9728 metadata are served without a credential,
and the binary limits them itself: one bucket per client address for the three,
20 requests a second with 200 in hand, refused `429` past it. Behind a proxy that
the server is not told to believe, every caller arrives from the proxy's
address, so the whole deployment shares that one bucket. Set
`--trusted-proxy-header` and `--trusted-proxies` so each caller is counted on its
own, or, when the proxy or a CDN already limits these paths, turn the server's
bucket off with `--discovery-rate-limit-rps=0`. The figures and what the bucket
counts are in
[Rate limiting the discovery documents](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#rate-limiting-the-discovery-documents).

> **The metadata path is at the host root**
>
> Started with `--public-url=https://mcp.example.com/gitlab`, the server answers:
>
> | Path | Result |
> | --- | --- |
> | `/gitlab/health`, `/health` | `200` |
> | `/gitlab/server-card`, `/server-card` | `200` |
> | `/gitlab/mcp/server-card`, `/mcp/server-card` | `200` |
> | `/.well-known/oauth-protected-resource` | `404` |
> | `/.well-known/oauth-protected-resource/gitlab` | `200` |
> | `/gitlab/.well-known/oauth-protected-resource/gitlab` | `404` |
> | `/.well-known/ai-catalog.json` | `404` |
>
> `/health` and the server card are mounted under the prefix as well as at the
> root, and the card after the endpoint's `/mcp` form too, because the server-card
> extension appends its suffix to the endpoint's URL rather than to the host: a
> client configured with `.../gitlab/mcp` finds the card at
> `.../gitlab/mcp/server-card`. The AI Catalog is not the server's to answer: it
> lists what the whole host publishes, and a deployment that wants one serves it
> from the proxy (see
> [Publishing an AI Catalog](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#publishing-an-ai-catalog)).
> The OAuth metadata is not mounted under the prefix, and the `401` challenge
> points at the derived form. The path-less form is not served either: it is the
> document of the resource that _is_ the origin, which a server living under a
> path is not. A proxy that routes only `/gitlab` answers `404` for the document
> every OAuth client fetches first, and discovery fails before the MCP endpoint is
> ever reached.
>
> Route the derived path, exactly and never as a prefix, to the same upstream
> without rewriting it. The derivation, and why a prefix answers for the other
> servers on the same host, are in
> [Where the metadata lives](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#where-the-metadata-lives-host-root-or-sub-path).

Two more that bite. **Do not let the proxy speak CORS:** the server answers its
own preflight from `--trusted-origins`, to which the `--public-url` origin is
added automatically, and a proxy that also emits `Access-Control-Allow-Origin`
produces two of them, which browsers reject outright while `curl` reports a
cheerful `200`, so it fails only in the one client that matters. `test/e2e/http`
pins that collision with a real nginx, and serves the nginx, Caddy, Traefik and
Apache blocks of this page through the proxy each is written for, to check that
the server's header is the only one; the server's side of it is in
[Cross-origin protection](https://docs.jmrp.io/gitlab-mcp-server/operations/security/#cross-origin-protection).
The one exception is a document the proxy serves itself rather than forwards:
the AI Catalog in the nginx example never reaches the server, so the proxy is
the only thing that can send its CORS header. And **anything the proxy does not
route is a `404`, not a `401`**, because the MCP endpoint is mounted on specific
patterns rather than as a catch-all: a path the server does not serve answers
`{"error":"not found"}` without asking for a credential. A `404` where you
expected a `401` usually means the path never reached the handler.

- **nginx**

  ```nginx e2e=nginx
  upstream gitlab_mcp {
      server 127.0.0.1:8080;
      keepalive 16;
  }

  server {
      listen 443 ssl;
      http2 on;
      server_name mcp.example.com;

      ssl_certificate     /etc/letsencrypt/live/mcp.example.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/mcp.example.com/privkey.pem;

      # RFC 9728 metadata: the exact path --public-url derives, at the host
      # root, never a prefix and never rewritten. This deployment owns its
      # host name, so it derives the bare path.
      location = /.well-known/oauth-protected-resource {
          proxy_pass http://gitlab_mcp;
          proxy_http_version 1.1;
          proxy_set_header Host       $host;
          proxy_set_header Connection "";
      }

      # AI Catalog: the host's own document, served here and never forwarded.
      # `types { }` clears the extension map, without which mime.types sends
      # the file as application/json and default_type is never reached.
      location = /.well-known/ai-catalog.json {
          alias /etc/nginx/ai-catalog.json;
          types { }
          default_type application/ai-catalog+json;
          add_header Access-Control-Allow-Origin "*" always;
          add_header Cache-Control "public, max-age=3600" always;
      }

      location / {
          proxy_pass http://gitlab_mcp;
          proxy_http_version 1.1;
          proxy_set_header Host              $host;
          proxy_set_header X-Real-IP         $remote_addr;
          proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          # With keepalive above: clear Connection so upstream connections are reused.
          proxy_set_header Connection        "";

          # Server-sent events must not be held.
          proxy_buffering off;
          proxy_request_buffering off;
          proxy_read_timeout 1h;
          proxy_send_timeout 1h;
      }
  }
  ```

  Pair it with `--trusted-proxy-header=X-Real-IP --trusted-proxies=127.0.0.1`.
  The list is what makes the header worth reading: it is believed only on a
  connection from one of those addresses, and a request from anywhere else is
  charged to its own peer address whatever header it carries. Without the list a
  caller who reaches the listener directly would write the header themselves and
  choose the address their failures are charged to, which is why the server
  refuses to start with one flag and not the other.

  Either header works. With `X-Forwarded-For` the server walks the value from the
  right, skipping every hop that is itself in the list, and charges the first hop
  that is not: with one nginx in front that is the client, and with a second proxy
  in front of nginx it is still the client, provided both proxies are listed. A
  value the client invented on the left is never reached, and a hop that is not
  an address charges the peer instead. A header that arrives as several field
  lines is read as one list, the lines joined in order: nginx extends the
  client's line, while HAProxy's `option forwardfor` appends a line of its own
  after it, and both end with the address the proxy heard from on the right.
  `X-Real-IP` set from `$remote_addr` at the
  hop nearest the server carries one address and needs no walk, and it is the
  header the project's own end-to-end proxy test uses.

  nginx passes client request headers upstream by default, so `Authorization`,
  `PRIVATE-TOKEN` and `GITLAB-URL` arrive with no `proxy_set_header` line. The
  one nginx habit to know is that it drops headers containing underscores unless
  `underscores_in_headers on` is set; none of this server's headers contains one.
  `Host` is the exception to passing through: nginx sends its upstream's name
  unless told otherwise, which is why both locations that forward set it. They
  clear `Connection` for a similar reason: nginx sends `Connection: close`
  upstream unless told otherwise, which would close every connection `keepalive`
  is there to reuse.

  The server marks every stream `X-Accel-Buffering: no`, which turns nginx's
  buffering off for that response by itself, and an nginx that buffers still
  passes an event on as soon as it reads one. `proxy_buffering off` is for the
  case where both fail: a hop in front of the server that drops the header, and a
  `gzip` whose `gzip_types` lists `text/event-stream`. Compression with buffering
  on holds the stream until the compressor has enough to write, which for a
  quiet `subscriptions/listen` is never; with buffering off nginx flushes the
  compressor on every read, so no event waits for the server to write another.

- **Caddy**

  ```text e2e=caddy
  # Caddyfile
  mcp.example.com {
      reverse_proxy 127.0.0.1:8080 {
          flush_interval -1
          transport http {
              read_timeout 1h
          }
      }
  }
  ```

  Caddy obtains and renews the certificate itself and forwards request headers
  unchanged, so most of the list above needs no configuration. `flush_interval -1`
  disables response buffering explicitly: Caddy already flushes immediately for
  `text/event-stream`, and `-1` means the behaviour no longer depends on the
  upstream getting its content type right.

  Caddy sets `X-Forwarded-For` by default, so
  `--trusted-proxy-header=X-Forwarded-For --trusted-proxies=127.0.0.1` pairs with
  this. A single site block covers the whole host, so the well-known path is
  already routed.

- **Traefik**

  With the Docker provider, on the container from the Compose block above:

  ```yaml e2e=traefik-labels
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.mcp.rule=Host(`mcp.example.com`)"
        - "traefik.http.routers.mcp.entrypoints=websecure"
        - "traefik.http.routers.mcp.tls.certresolver=le"
        - "traefik.http.services.mcp.loadbalancer.server.port=8080"
  ```

  Traefik does not buffer responses unless a `buffering` middleware is added, so
  the fix here is to not add one. What does need attention is the entry point's
  responding timeout, which is global rather than per route:

  ```yaml e2e=traefik-static
  # traefik static configuration
  entryPoints:
    websecure:
      address: ":443"
      transport:
        respondingTimeouts:
          readTimeout: 0
          idleTimeout: 3m
  ```

  `readTimeout: 0` removes the limit on how long a request may take, which is
  what a long-lived stream needs. Traefik sets `X-Forwarded-For` when it is the
  edge, or when the previous hop is listed in `forwardedHeaders.trustedIPs`, so
  `--trusted-proxy-header=X-Forwarded-For --trusted-proxies=127.0.0.1` pairs
  with this, the address being the one Traefik connects from.

  The router's `tls.certresolver=le` names a certificate resolver that the static
  configuration has to define, under `certificatesResolvers.le`, with the ACME
  details of your account. The block above defines none: Traefik then logs that
  the router uses a nonexistent certificate resolver and serves its own default
  certificate for the host, which every client refuses.

  A `Host()` rule matches every path on the host, so the well-known document is
  covered. A `PathPrefix()` rule is where the trap opens: a server routed by
  `` PathPrefix(`/gitlab`) `` and started with
  `--public-url=https://mcp.example.com/gitlab` publishes its metadata at
  `/.well-known/oauth-protected-resource/gitlab`, outside that prefix. Add a
  second router for exactly that path,
  `` Path(`/.well-known/oauth-protected-resource/gitlab`) ``, pointing at the
  same service.

- **Apache**

  ```apache e2e=apache
  <VirtualHost *:443>
      ServerName mcp.example.com

      SSLEngine on
      SSLCertificateFile    /etc/letsencrypt/live/mcp.example.com/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/mcp.example.com/privkey.pem

      ProxyPreserveHost On
      ProxyPass        / http://127.0.0.1:8080/ timeout=3600 connectiontimeout=10
      ProxyPassReverse / http://127.0.0.1:8080/

      RequestHeader set X-Forwarded-Proto "https"
  </VirtualHost>
  ```

  The block sets no flush option, and the stream needs none: `mod_proxy_http`
  passes what it has read on to the client whenever its next read from the
  server would wait, so httpd 2.4 never holds an event while it waits for the
  server to write another. Events the server writes back to back, faster than
  httpd reads them, can reach the client together. `flushpackets=on` would
  change neither: the [`ProxyPass` reference](https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass)
  says it is in effect only for `mod_proxy_ajp` and `mod_proxy_fcgi`, and
  `mod_proxy_http` never reads it. `timeout=3600` is this route's read timeout,
  separate from the server-wide `Timeout`.

  Leave authentication to the MCP server, so nothing here should sit behind
  `Require valid-user`; Apache forwards `Authorization` upstream when it is not
  consuming it itself. `mod_proxy_http` sets `X-Forwarded-For` on every request
  it forwards, so `--trusted-proxy-header=X-Forwarded-For --trusted-proxies=127.0.0.1`
  pairs with this. `mod_remoteip` with `RemoteIPHeader X-Forwarded-For` fixes the
  address in Apache's own logs, which is a separate matter: the MCP server reads
  the header itself.

  Modules required: `proxy`, `proxy_http`, `ssl`, `headers`, and `remoteip` if you
  use it, on top of the ones every stock configuration loads, among them an MPM
  and the authorization core, without which httpd refuses every request.

- **Cloudflare Tunnel**

  A tunnel replaces the inbound listener entirely: `cloudflared` dials out, so the
  host needs no open port and no certificate of its own.

  ```yaml e2e=cloudflared
  # ~/.cloudflared/config.yml
  tunnel: <tunnel-uuid>
  credentials-file: /etc/cloudflared/<tunnel-uuid>.json

  ingress:
    - hostname: mcp.example.com
      service: http://127.0.0.1:8080
      originRequest:
        connectTimeout: 10s
        disableChunkedEncoding: false
        noTLSVerify: false
    - service: http_status:404
  ```

  `disableChunkedEncoding: false` is the default, spelled out. The setting governs
  the request `cloudflared` sends to the origin, for an origin that cannot read a
  chunked request body, and not the origin's response, so the SSE stream does not
  depend on it either way.

  Cloudflare sets `CF-Connecting-IP`, so name that one:
  `--trusted-proxy-header=CF-Connecting-IP --trusted-proxies=127.0.0.1`, since
  `cloudflared` connects from the same host. Prefer it over `X-Forwarded-For`
  here, because a client cannot forge it through the edge: the edge writes it on
  every request it forwards, and refuses a request that already carries one with
  a `403` and its error 1000, before the request reaches the tunnel.

  Two Cloudflare behaviours to check before blaming the server. The proxy applies
  an inactivity limit to a response, and what keeps a quiet `subscriptions/listen`
  stream under it is the server's keep-alive: an SSE comment (`: keep-alive`)
  written into the response body after 25 seconds without a write, not a
  TCP-level keep-alive, so it reaches the edge only if nothing on the way holds
  the body back. A tool call that writes nothing until its result gets no
  keep-alive, so one silent for longer than that limit, 125 seconds unless an
  Enterprise plan raised it, is cut too, and the client is answered with a `524`.
  And Cloudflare's own CORS and caching rules, if enabled for the hostname, land
  in exactly the same collision as a proxy-level `Access-Control-Allow-Origin`.

### On a LAN, in plain HTTP

Every block above serves a public name with a certificate. A proxy that serves
a LAN in plain HTTP, reached as `http://mcp.lan` by the machines on the same
network, is the same proxy without the certificate, and it needs one change on
the server's side: `--public-url` takes an `https` origin, or `http` on loopback
only, so a plain LAN name cannot be declared with it. The host is admitted by
trusting the hop instead. The proxy and the server share the machine, the
server listens on loopback, and `--trusted-proxies=127.0.0.1` vouches for the
connection the proxy makes, from which any forwarded host is served:

```nginx e2e=nginx-lan
upstream gitlab_mcp {
    server 127.0.0.1:8080;
    keepalive 16;
}

server {
    # The LAN address of this machine, in plain HTTP.
    listen 192.168.1.10:80;
    server_name mcp.lan;

    location / {
        proxy_pass http://gitlab_mcp;
        proxy_http_version 1.1;
        proxy_set_header Host       $host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Connection "";

        proxy_buffering off;
        proxy_request_buffering off;
        proxy_read_timeout 1h;
        proxy_send_timeout 1h;
    }
}
```

```bash e2e=nginx-lan-server run=background
gitlab-mcp-server --http --http-addr=127.0.0.1:8080 \
  --gitlab-url=https://gitlab.example.com \
  --trusted-proxy-header=X-Real-IP --trusted-proxies=127.0.0.1
```

The pairing that admits the host is also the one that charges each LAN client's
failures to that client in the failure limiter, rather than to the proxy, as in
the nginx tab above. What the plain hop gives up is the encryption of
everything on it: each request's token crosses the LAN readable to anything on
its path, so this shape is for a network trusted as much as the tokens are.
OAuth mode is not available on it, since the resource identifier OAuth clients
are given is the `--public-url` origin, and that must be `https`.

With the proxy on another machine, the server's own listener is the one off
loopback in plain HTTP. Startup then warns, naming its address;
`--allow-plaintext-http` declares that plain hop deliberate, and
`--allow-plaintext-http=false` (or `GITLAB_MCP_ALLOW_PLAINTEXT_HTTP=false`)
refuses to start it. `--trusted-proxies` then names the proxy's LAN address
rather than `127.0.0.1`
([Plain HTTP on an address other machines can reach](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#plain-http-on-an-address-other-machines-can-reach)).

## TLS between the proxy and the server

When the proxy shares the machine, remove the hop rather than encrypting it:
`--http-addr=/run/gitlab-mcp/server.sock`. When the proxy is elsewhere, the
binary terminates TLS itself and no sidecar is needed:

```bash run=background
gitlab-mcp-server --http --http-addr=:8443 \
  --tls-cert=/etc/ssl/mcp.crt --tls-key=/etc/ssl/mcp.key \
  --gitlab-url=https://gitlab.example.com
```

Both flags or neither: `--tls-cert requires --tls-key` is a startup error, not a
warning. TLS 1.2 is the floor with no ceiling, so a current proxy negotiates 1.3
and anything below 1.2 is refused. The mechanics of both listeners are in
[Listening on a unix socket, or on TLS](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#listening-on-a-unix-socket-or-on-tls).

> **A renewed certificate needs no restart**
>
> The pair is loaded at startup, strictly: a missing file or a key that does not
> match its certificate stops the start. After that the certificate is served
> through a callback that checks both files on each handshake and re-reads them
> when either has changed, so a renewal that writes the new pair over the same
> paths is presented on the next handshake, while connections already open keep
> the old one until they are replaced. A half-written or unreadable pair keeps the
> previous certificate in service and is logged once. There is no reload signal
> and none is needed: the only signals handled are `SIGINT` and `SIGTERM`, both of
> which mean shutdown, and a `SIGHUP` terminates the process. The renewal hook
> and the properties the tests pin are in
> [Certificate rotation without downtime](https://docs.jmrp.io/gitlab-mcp-server/enterprise/tls/#certificate-rotation-without-downtime).

## Direct exposure, no proxy

The binary answers its own CORS preflights, its own `404`s, its own security
headers, and can terminate TLS or listen on a unix socket. A deployment without
a proxy should still be deliberate about five things:

- **`--auth-mode=oauth`.** Bearer verification against the instance the request
  selected, with RFC 9728 metadata so clients discover it. `--public-url` is
  required and must be the externally reachable https origin.
- **Certificates and their renewal.** Re-read on the handshake after the files
  change, so a renewal is a file write rather than a restart; see
  [TLS between the proxy and the server](https://docs.jmrp.io/gitlab-mcp-server/operations/remote-deployment/#tls-between-the-proxy-and-the-server).
- **Rate limiting.** `--rate-limit-rps` defaults to 10 in HTTP mode with a burst
  of 40, counted per pooled token rather than per address. Two things are
  counted per address and answer `429` with `Retry-After`: the budget on
  authentication failures, and the bucket on the documents served without a
  credential, the two server cards and the RFC 9728 metadata, 20 requests a
  second with 200 in hand. A throttled `tools/call` is not a `429`: it is a
  normal MCP result carrying an error, so it will not show up in HTTP-level
  monitoring.
- **Bounds.** `--max-http-clients` caps pooled entries, not sessions or the
  requests they hold; the process bounds the calls held at once across every
  credential by its descriptor limit (192 under a hard limit of 1024), not
  configurable, and refuses the next one as busy (see
  [Requests held open at once](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#requests-held-open-at-once)).
  `--pool-idle-timeout` reclaims unused entries, except one with a live
  subscription, which is not idle however long ago it made a request.
  `--session-timeout` applies to stateful mode only, where the process also
  keeps at most half as many sessions as it may hold calls (96 under a hard
  limit of 1024) and refuses the next `initialize` as busy (see
  [Stateful sessions kept at once](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#stateful-sessions-kept-at-once)).
  Neither ceiling has a per-credential one beside
  it, since a credential is a key a caller can mint, so one credential can fill
  either; and the server keeps no memory cap of its own, so keep `MemoryMax` on
  the unit (the unit above sets 512M), or set a memory limit on the container,
  which is what bounds memory where the descriptor limit is large.
- **The token passes through the box.** Every caller's GitLab token reaches this
  process, authenticates one request, and is never persisted. Whether the people
  whose tokens they are consider the machine trustworthy is a question the
  software cannot answer, so ask them rather than answering for them.

## Several instances behind one proxy

> **At scale**
>
> Serving hundreds of people from one deployment has its own section:
> [Enterprise Deployments](https://docs.jmrp.io/gitlab-mcp-server/enterprise/overview/) covers sizing
> from the measurements, consistent hashing across a changing instance set,
> health-driven ejection, MCP gateways, certificate rotation and operations at
> scale. This section is the first pass those build on.

### Affinity is a preference, with two exceptions

Each instance keeps two caches. Both are per process, both are keyed on the
caller, and neither can be shared:

- The **server pool**, keyed on `SHA-256(token + "\x00" + gitlabURL)`. On a miss
  the entry is built: the credential is verified, its scopes are read, the
  instance's licensing tier is detected, and the entry is bound to the MCP
  server of its configuration shape. The tool catalog is **not** built per
  credential: it is registered once per shape and shared by every credential of
  that shape
  ([ADR-0020](https://github.com/jmrplens/gitlab-mcp-server/blob/main/docs/development/adr/adr-0020-one-server-per-configuration-shape.md)),
  so its build (the first client of a shape waited 0.32 seconds for it on the
  dynamic surface and 1.28 on individual in the
  [resource benchmark](https://docs.jmrp.io/gitlab-mcp-server/performance/resource-benchmark/#what-a-client-waits-for),
  on the host it states) is paid once per shape on each instance rather than
  once per caller. The handshake is answered before the catalog exists, so even
  that first build lands on the first request that needs the catalog rather
  than on `initialize`.
- The **OAuth identity cache**, keyed on instance and token, with
  `--oauth-cache-ttl` at 15 minutes by default. A miss is a round trip to GitLab
  to verify the credential.

Both hold live objects rather than serializable state, so there is no version of
this where a second instance reads the first one's cache. The question for a
balancer is therefore not whether requests will work anywhere: under the default
`--stateless=true` every POST is self-contained and every instance answers
correctly. The question is how many times you are willing to pay for a cache
designed to be paid once, and since its expensive half is shared per
configuration rather than held per credential, the answer is small: a
credential probe, a licensing lookup and a client.

Two things do not merely cost extra when a caller moves, they break. A stateful
session (`--stateless=false`) lives in the process that minted its
`Mcp-Session-Id`, and another instance answers that id with `404`, which a
client reads as its session having been terminated. Resource subscriptions are
watchers held by one process and disappear with it. If you run either, affinity
is not an optimization.

### The three distributions

| Distribution | Key | What it costs |
| --- | --- | --- |
| Round robin | none | Every instance eventually holds an entry for every caller, so each caller's credential probe, licensing lookup and token verification are paid once per instance rather than once. The catalog behind those entries is per configuration shape, so it is not multiplied. Correct, and pays N times for the cheap half |
| IP hash | the client address | Free, one directive, no secret to keep. The key is the wrong grain in both directions: an office, a VPN concentrator or a CI runner fleet is one address, so all of it collapses onto one instance, while one caller roaming between networks changes key and lands cold each time. Behind a CDN it needs the real address recovered first |
| Token hash | a salted digest of the credential | Matches the grain of what is cached, because the pool key is derived from the token. Costs one secret to manage, a fallback for credential-less requests, and one cold entry whenever a caller rotates its token. This is what the hosted endpoint runs |

Token hash is the right answer here, and the reason is narrow enough to state
exactly: the thing being cached is keyed on the token, so the routing key should
be too. Neither of the others is wrong, and both are fine wherever one instance
serves the whole caller population, which is most deployments. Reach for a
second instance for availability, not for throughput: the ceiling that usually
binds is GitLab's own rate limit on each user, whose every token spends it, and
on GitLab.com its limit on the deployment's egress address, neither of which
more instances behind that address raise
([What GitLab counts](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#what-gitlab-counts)).

### Hashing the token without routing on it

The credential must not become the routing key. An affinity key ends up in the
balancer's memory, in its access log if the log format names the variable, and
in whatever upstream selection it drives. A raw bearer token in any of those is
a credential leak with extra steps.

The fix is a salted digest: hash a per-deployment secret together with the
credential, and route on that. The digest is a distribution function rather than
a security primitive; the salt is what does the security work. Without it the
key is a token fingerprint that anyone holding a candidate token could confirm.
With it the key is meaningless outside this deployment and cannot be replayed as
a credential.

Three details decide whether the affinity actually holds:

- **Normalize before hashing.** Strip the `Bearer` scheme prefix and surrounding
  whitespace. The same token spelled two ways hashes two ways and lands on two
  instances.
- **Fall back to the address, not to nothing.** A request with no credential
  has no key, and an empty key makes every anonymous request hash identically
  onto one instance. `$remote_addr` spreads them.
- **Use consistent hashing.** Removing one of three instances then relocates
  roughly a third of the callers instead of reshuffling all of them, which is
  the difference between a rolling update that warms one cold pool and one that
  warms every pool.

### A worked nginx balancer

`hash` accepts a string with variables and hashes it itself, so the salt goes
into the key expression without ever becoming a loggable variable of its own.
This needs no third-party module:

```nginx e2e=nginx-balancer
# Keep the salt out of the repository: include it from a 0600 file.
map $host $mcp_salt {
    default "a-long-random-per-deployment-salt";
}

# The bearer credential, without its scheme prefix.
map $http_authorization $mcp_bearer {
    default                     "";
    "~*^Bearer[ ]+(?<tok>\S+)$" $tok;
}

# Legacy-mode callers send PRIVATE-TOKEN instead.
map $mcp_bearer $mcp_credential {
    default $mcp_bearer;
    ""      $http_private_token;
}

# No credential: fall back to the client address.
map $mcp_credential $mcp_affinity {
    default $mcp_credential;
    ""      $remote_addr;
}

upstream gitlab_mcp {
    hash "$mcp_salt$mcp_affinity" consistent;
    server 10.0.0.11:8080 max_fails=2 fail_timeout=10s;
    server 10.0.0.12:8080 max_fails=2 fail_timeout=10s;
    server 10.0.0.13:8080 max_fails=2 fail_timeout=10s;
    keepalive 32;
}

server {
    listen 443 ssl;
    server_name mcp.example.com;

    ssl_certificate     /etc/letsencrypt/live/mcp.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mcp.example.com/privkey.pem;

    location / {
        proxy_pass http://gitlab_mcp;
        proxy_http_version 1.1;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection        "";

        proxy_buffering off;
        proxy_request_buffering off;
        proxy_read_timeout 1h;

        # Retry only what was never delivered.
        proxy_next_upstream error timeout;
        proxy_next_upstream_tries 2;
    }

    # The exact derived metadata path, never a prefix.
    location = /.well-known/oauth-protected-resource {
        proxy_pass http://gitlab_mcp;
        proxy_http_version 1.1;
        proxy_set_header Host       $host;
        proxy_set_header Connection "";
    }
}
```

Do not log `$mcp_affinity` or `$mcp_credential`. Where an explicit digest is
preferred, `set_md5 $key "$mcp_salt$mcp_affinity"` from
`ngx_http_set_misc_module` (OpenResty, or Debian and Ubuntu's `nginx-extras`)
produces one, and njs does the same in a few lines. The hosted endpoint at
`mcp.jmrp.io` computes `md5(salt + bearer)` that way.

The same balancer for HAProxy is on the
[load balancing page](https://docs.jmrp.io/gitlab-mcp-server/enterprise/load-balancing/#haproxy).
HAProxy computes the salted digest in its own configuration, and it polls
`/health`, which nginx open source cannot: it is the one of the two that takes
a draining instance out before its listener closes.

> **Never add `non_idempotent` to `proxy_next_upstream`**
>
> Without it, nginx retries a `POST` on another instance only when the request was
> never sent, which is exactly the guarantee wanted here: a `tools/call` that
> created an issue and then lost its response must not be replayed on a second
> instance. `error timeout` on its own is connection-level, and without
> `non_idempotent` no condition, `http_500` included, passes on a `POST` that was
> sent. Adding `non_idempotent`, with any condition, turns one delivered mutating
> call into two.

Prove the affinity rather than assuming it. Two instances, eight tokens, six
requests each, reading the backend out of the access log:

```bash
for t in alpha bravo charlie delta echo foxtrot golf hotel; do
  for _ in $(seq 1 6); do
    curl -s -o /dev/null -H "Authorization: Bearer token-$t" \
      https://mcp.example.com/health
  done
done
```

Every token must show exactly one backend across its six requests, and the eight
tokens must not all show the same one. Run the same loop with no `Authorization`
header and with `PRIVATE-TOKEN` instead, to confirm both fallbacks pin as well.

### Rolling updates, probes and egress

**Announce the drain before the listener closes.** On `SIGTERM` the process
marks itself draining first: from that moment `/health` answers `503` with
`"status": "draining"` and `Cache-Control: no-store`. By default the listener
closes right after, so a balancer polling `/health` usually notices the closed
listener rather than the `503`, one probe later, and every request it sent in
that window failed. Start each instance with `--drain-delay` set to at least
one probe interval (`--drain-delay=10s` for a 5-second probe that tolerates two
failures): the listener then stays open that long answering `503`, the balancer
removes the backend, and only then does the listener close and the in-flight
requests get their 15 seconds to finish before the remaining connections are
closed. Under consistent hashing the callers pinned to it move to a neighbour,
warm an entry there, and come back when it returns. A balancer that cannot poll
still works the old way: remove the backend by hand, let streams drain, then
signal.

**`/health` is the probe, and it is honest about what it knows.** No credential,
no GitLab round trip, `200` with `status`, `version`, `commit`, `build`,
`config_digest`, `started_at` and `uptime_seconds`. `status` is `ok` while
serving and `draining` once shutdown was requested; the HTTP status carries the
same verdict. `build` is the label to put on a dashboard, the closest release
plus the short commit, so a release binary and a build from `main` read alike.
`config_digest` is the fleet check: every instance behind one balancer must
report the same one, or one of them serves a different catalog to whichever
clients reach it and nothing else notices. Compare it in the same loop that
proves the affinity. The endpoint deliberately does not test GitLab
reachability, so a balancer must not read a `200` as "GitLab is up", and there
is no separate readiness endpoint. Every field is described in
[Health check](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/#health-check).

**Give the fleet a fixed egress address.** GitLab applies its own rate limits and
any IP allow-lists per source address. Instances behind a NAT gateway with a
stable address are one caller to GitLab; instances with ephemeral public
addresses are several unpredictable ones, and an allow-list cannot be written
for them.

**Remember the limiter multiplies.** `--rate-limit-rps` is per pooled token
entry inside one process, so three instances mean up to three buckets for one
caller unless affinity pins it to one. With token affinity the configured
number is the number. Under round robin, either divide it by the instance count
or put the real limit on the balancer.

**Revalidation keeps running per instance.** `--revalidate-interval` re-checks
pooled credentials every 15 minutes by default and evicts the ones GitLab now
rejects; setting it to `0` does not make a revoked token last forever, because
an entry older than an hour is rebuilt on next use anyway. That is per instance,
so the same token is re-checked once per instance holding it, which is one more
thing affinity keeps to one.

## Further reading

- [Enterprise Deployments](https://docs.jmrp.io/gitlab-mcp-server/enterprise/overview/) for sizing from measurements, balancing at scale, MCP gateways, certificate rotation and operations
- [HTTP Server Mode](https://docs.jmrp.io/gitlab-mcp-server/operations/http-server/) for every flag, the server pool, and the OAuth derivation rules
- [OAuth Application](https://docs.jmrp.io/gitlab-mcp-server/operations/oauth-app/) for creating the GitLab application clients authorize against
- [Security](https://docs.jmrp.io/gitlab-mcp-server/operations/security/) for the threat model, CORS, and the hardening checklist
- [Sizing a Deployment](https://docs.jmrp.io/gitlab-mcp-server/performance/sizing/) for what a credential, a configuration shape and a call cost
- [OpenTelemetry](https://docs.jmrp.io/gitlab-mcp-server/operations/telemetry/) for what an instance can report about itself
- [Troubleshooting](https://docs.jmrp.io/gitlab-mcp-server/operations/troubleshooting/) for symptoms and their usual causes

## Frequently asked questions

### Can I load-balance several instances round-robin?

For ordinary calls it works, and it pays for the cheap half of the cache several times. Each instance keeps a server pool keyed on a hash of the token and GitLab URL, and an OAuth identity cache keyed on the instance and token. Both are per process and hold live objects, so nothing is shared between instances. A caller that lands on a different instance each request has a pool entry built there (its scopes read and the licensing tier detected) and its token verified against GitLab again. The tool catalog is not rebuilt for it: an instance registers the catalog once per configuration shape and shares it with every credential of that shape. Round robin is correct; it just pays N times. Two things do need affinity, because they live in one process: legacy stateful sessions and resource subscriptions. Token affinity pins a caller to the instance that already holds its entry.

### Why not just use IP hash for affinity?

Because the address is the wrong grain. An office, a VPN concentrator or a CI runner fleet is one address, so all of it collapses onto one instance; and one caller roaming between networks changes key and lands on a cold instance each time. Behind a CDN the balancer also has to recover the real address first. IP hash is cheap and needs no secret, which is a real advantage, but the thing being cached is keyed on the token, so the routing key should be too.

### Do I need a reverse proxy at all?

No. The binary answers its own CORS preflights, its own 404s, its own security headers, terminates TLS with `--tls-cert` and `--tls-key`, and presents a renewed certificate on the next handshake without a restart. Where a proxy shares the machine, `--http-addr=/run/gitlab-mcp/server.sock` removes the hop instead of encrypting it. A proxy earns its place for obtaining certificates automatically, for hosting other services on the same name, and for balancing several instances.

### My OAuth clients get a 404 during discovery. What is wrong?

The proxy is almost certainly routing only the path prefix. The RFC 9728 metadata document lives at the **host root**, not under `--public-url`'s path: started with `--public-url=https://mcp.example.com/gitlab`, the server serves it at `/.well-known/oauth-protected-resource/gitlab` and answers 404 for `/gitlab/.well-known/oauth-protected-resource/gitlab`. `/health` and the server card *are* mounted under the prefix as well; the OAuth metadata is not. Route that exact derived path, `/.well-known/oauth-protected-resource/gitlab`, to the same upstream without rewriting it, and never a prefix of it.
