Skip to content

Reverse proxy and the client's address#

piler-ui has no TLS: it runs behind a reverse proxy (nginx, Traefik, Caddy, ...) that terminates TLS and forwards the requests over plain HTTP. piler-ui then needs two things from the proxy:

  • the original scheme and host (X-Forwarded-Proto, X-Forwarded-Host): the CSRF checks, the cookies and the passkeys
  • the client's address (X-Forwarded-For, or X-Real-IP): the audit log, the logs, the failed login lockout, the rate limiter, the allowed addresses of the API tokens and of /metrics

Settings#

Setting Default
TRUSTED_PROXIES 127.0.0.1 The proxies' addresses or networks, comma separated. Only their forwarding headers count
PROXY_HEADER X-Forwarded-For The header with the client's address: X-Forwarded-For or X-Real-IP

List the proxy itself, not a whole private range: any host in TRUSTED_PROXIES can pose as the proxy and set the client's address. In Docker, give the proxy and piler-ui a network of their own and list that network (e.g. 172.30.0.0/24), or the proxy's fixed address.

TRUSTED_PROXIES is needed even if the proxy cleans the headers: a request that bypasses the proxy (piler-ui's port reachable directly, e.g. published on 0.0.0.0 in Docker) may send any X-Forwarded-For.

How piler-ui finds the client#

  • A request without forwarding headers is a direct connection: the client is the connection's address.
  • From an address in TRUSTED_PROXIES, the client is the right-most X-Forwarded-For address that isn't a trusted proxy. The left-most is the client's to write: a proxy that appends to the header puts the real address at the end. With PROXY_HEADER=X-Real-IP the header must have a single address.
  • Forwarding headers from an address not in TRUSTED_PROXIES, or from a trusted proxy without a usable PROXY_HEADER, are ignored: the audit and the logs get the connection's address, and the API tokens with allowed addresses and /metrics refuse the request. The health page warns about such requests (the counts are per piler-ui process since its start), and about the ranges in TRUSTED_PROXIES wider than /16 (IPv4) or /48 (IPv6).

Examples#

nginx#

nginx passes the client's X-Forwarded-For through unless it's set. Replace it with the connection's address:

location / {
    proxy_pass http://piler-ui:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
}

The common $proxy_add_x_forwarded_for appends to the client's value; it works too, since piler-ui reads the header from the right. Or use proxy_set_header X-Real-IP $remote_addr; with PROXY_HEADER=X-Real-IP.

Traefik#

Traefik drops the X-Forwarded-* headers of the clients not in the entrypoint's forwardedHeaders.trustedIPs and sets them itself: no extra configuration. Don't set forwardedHeaders.insecure.

Caddy#

Caddy 2.5+ ignores the forwarded headers of the clients outside its trusted_proxies and sets the real address:

archive.example.com {
    reverse_proxy piler-ui:3000
}

Proxy on the host, piler-ui in Docker#

With the proxy on the host (e.g. Traefik as a service) and piler-ui in a container, piler-ui sees the proxy's connections from the gateway of its Docker network, through a published port or straight to the container's address. That is docker0 (172.17.0.1) only on the default bridge: Docker Compose makes a network of its own (172.18.0.1, 172.19.0.1, ...). The gateway of piler-ui's networks:

docker inspect piler-ui --format '{{range $n, $c := .NetworkSettings.Networks}}{{$n}} {{$c.Gateway}}{{"\n"}}{{end}}'

List that single address, not the network:

TRUSTED_PROXIES=127.0.0.1,172.18.0.1

Publish piler-ui's port on the loopback only (127.0.0.1:3000:3000), or firewall it: a client connecting to the host's port directly comes from the same gateway address, and could set any X-Forwarded-For.

Checking#

Send a fake header through the proxy:

curl -H 'X-Forwarded-For: 1.2.3.4' https://archive.example.com/login

The client_ip of the request in the piler-ui log must be your own address, not 1.2.3.4 and not the proxy's.