Skip to content

TLS & Reverse Proxy

Which certificate goes where

Listener Port Certificate Renewed by
Dashboard, Ferrous API, Pi-hole API, DoH web port (8080) Towards clients, the reverse proxy's public certificate. Between the proxy and Ferrous DNS, the server's own self-signed one The proxy, through ACME. The self-signed one, once a year by hand
DoT, DoQ 853 Files on disk: [server.encrypted_dns] tls_cert_path and tls_key_path certbot and the deploy hook

The server reads certificates once, at startup. A new certificate on disk, an upload or a generated one takes effect after a restart, and a restart starts with an empty cache (see Sizing). In testing, DoT answered with the renewed certificate 0.4–0.5 s after the restart.


Dashboard behind a reverse proxy

The proxy ends the public HTTPS connection and talks HTTPS to Ferrous DNS too. That second hop matters. The session cookie is marked Secure only when the server itself serves HTTPS, never because a proxy says so in X-Forwarded-Proto. Over plain HTTP behind the proxy, the browser would get a session cookie it is allowed to send in the clear.

1. Turn on HTTPS in Ferrous DNS

In Settings → Security, generate a self-signed certificate. POST /ferrous/api/v1/tls/generate does the same through the API. Then turn web TLS on and restart:

[server.web_tls]
enabled = true

The generated certificate is issued to localhost and ferrous-dns, and is valid for one year. Settings → Security shows when it expires, and every dashboard page shows a warning once the certificate the server is serving has less than 21 days left. Before then, generate a new one, restart, and refresh the proxy's copy (step 2). DoT and DoQ certificates in files of their own have no dashboard warning: rely on the alert rule for them.

2. Give the proxy a copy of the certificate

The proxies below check the server against its own certificate instead of turning verification off. Copy the certificate only, never the key:

sudo install -m 0644 /var/lib/ferrous-dns/tls/cert.pem /etc/nginx/ferrous-dns-upstream.pem

Or /etc/caddy/ or /etc/traefik/, to match the proxy.

docker cp ferrous-dns:/data/cert.pem ./ferrous-dns-upstream.pem

3. Configure the proxy

Replace dns.example.com with your host name. The examples reach Ferrous DNS at 127.0.0.1:8080, with the proxy on the same host. With both in containers on one Docker network (see step 4), use the container's name instead: https://ferrous-dns:8080. The certificate names ferrous-dns too, so the check still matches.

The public certificate comes from certbot (certbot --nginx -d dns.example.com). Install as /etc/nginx/conf.d/ferrous-dns.conf:

# Ferrous DNS (dashboard, both APIs and DoH) behind nginx.
# Guide: https://ferrous-networking.github.io/ferrous-dns/operations/tls-reverse-proxy/
# Install as /etc/nginx/conf.d/ferrous-dns.conf and replace dns.example.com.
#
# ferrous-dns serves HTTPS itself ([server.web_tls] with its own certificate),
# so the session cookie keeps its Secure flag. nginx checks that certificate
# against a copy of it (ferrous-dns-upstream.pem) instead of skipping the check.

server {
    listen 80;
    listen [::]:80;
    server_name dns.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    # nginx before 1.25.1: drop this line and write "listen 443 ssl http2;" above.
    http2 on;
    server_name dns.example.com;

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

    add_header Strict-Transport-Security "max-age=31536000" always;

    location / {
        proxy_pass https://127.0.0.1:8080;
        proxy_ssl_verify on;
        proxy_ssl_trusted_certificate /etc/nginx/ferrous-dns-upstream.pem;
        # The certificate ferrous-dns generates is issued to "localhost" and "ferrous-dns".
        proxy_ssl_name localhost;
        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;
    }
}

Caddy obtains and renews the public certificate itself:

# Ferrous DNS (dashboard, both APIs and DoH) behind Caddy.
# Guide: https://ferrous-networking.github.io/ferrous-dns/operations/tls-reverse-proxy/
# Replace dns.example.com; Caddy obtains and renews its certificate itself.
#
# ferrous-dns serves HTTPS itself ([server.web_tls] with its own certificate),
# so the session cookie keeps its Secure flag. Caddy checks that certificate
# against a copy of it (ferrous-dns-upstream.pem) instead of skipping the check.

dns.example.com {
    header {
        Strict-Transport-Security "max-age=31536000"
    }

    reverse_proxy https://127.0.0.1:8080 {
        transport http {
            tls_trust_pool file /etc/caddy/ferrous-dns-upstream.pem
            # The certificate ferrous-dns generates is issued to "localhost" and "ferrous-dns".
            tls_server_name localhost
        }
    }
}

The static configuration, /etc/traefik/traefik.yml:

# Traefik static configuration for Ferrous DNS.
# Guide: https://ferrous-networking.github.io/ferrous-dns/operations/tls-reverse-proxy/
# The routes are in traefik-dynamic.yml, loaded by the file provider below.

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
  websecure:
    address: ":443"

certificatesResolvers:
  letsencrypt:
    acme:
      email: admin@example.com
      storage: /etc/traefik/acme.json
      httpChallenge:
        entryPoint: web

providers:
  file:
    filename: /etc/traefik/traefik-dynamic.yml

The routes, /etc/traefik/traefik-dynamic.yml:

# Ferrous DNS (dashboard, both APIs and DoH) behind Traefik: the routes.
# Guide: https://ferrous-networking.github.io/ferrous-dns/operations/tls-reverse-proxy/
# Replace dns.example.com.
#
# ferrous-dns serves HTTPS itself ([server.web_tls] with its own certificate),
# so the session cookie keeps its Secure flag. Traefik checks that certificate
# against a copy of it (ferrous-dns-upstream.pem) instead of skipping the check.

http:
  routers:
    ferrous-dns:
      rule: Host(`dns.example.com`)
      entryPoints:
        - websecure
      middlewares:
        - ferrous-dns-headers
      service: ferrous-dns
      tls:
        certResolver: letsencrypt

  middlewares:
    ferrous-dns-headers:
      headers:
        stsSeconds: 31536000

  services:
    ferrous-dns:
      loadBalancer:
        serversTransport: ferrous-dns
        servers:
          - url: https://127.0.0.1:8080

  serversTransports:
    ferrous-dns:
      # The certificate ferrous-dns generates is issued to "localhost" and "ferrous-dns".
      serverName: localhost
      rootCAs:
        - /etc/traefik/ferrous-dns-upstream.pem

The proxy serves the whole site at the root of its own host name. The dashboard uses absolute paths, so it cannot live under a sub-path such as /ferrous/. Through that one host name it serves:

  • the dashboard at /;
  • the Ferrous API at /ferrous/api/v1;
  • the Pi-hole compatible API at /api;
  • DoH at /dns-query, once [server.encrypted_dns] doh_enabled = true (it is off by default);
  • /metrics, if enabled.

The dashboard polls with plain requests, so no WebSocket settings are needed.

The server sends its own frame, nosniff and Referrer-Policy headers (Security headers). The examples add HSTS, which only the proxy can promise for its public name. Do not add X-Frame-Options on the proxy as well: a second, different value makes browsers ignore the header.

4. Keep the dashboard port off the network

With a proxy in front, clients have no reason to reach port 8080 directly. When the proxy shares Ferrous DNS's network (bare metal, systemd, or Docker with host networking, as in the bundled compose file), bind the web server to loopback and leave DNS on the network:

[server]
bind_address     = "auto"
web_bind_address = "127.0.0.1"

Everything on the web port moves with it: the Pi-hole API, /metrics, and DoH without a doh_port of its own. See Dashboard on its own address.

When another host must still reach the port directly, such as a Prometheus that scrapes /metrics, keep the shared address and close the port in the firewall instead.

Load this with nft -f, or add it to your nftables configuration. Port 53 stays open. Add an accept line, before the drop, for every other host that must reach the port, such as the Prometheus that scrapes /metrics:

# Keep the dashboard port to this host: the reverse proxy reaches it over loopback.
table inet ferrous_dns {
    chain input {
        type filter hook input priority filter; policy accept;
        ip saddr 192.168.1.20 tcp dport 8080 accept   # Prometheus
        iifname != "lo" tcp dport 8080 drop
    }
}

On a bridge network, publish only port 53 and put the proxy on the same Docker network, so that it alone reaches 8080. Leave web_bind_address unset there: the proxy reaches the container by name, never over its loopback, and a published port cannot reach it either.

The bundled compose file uses host networking, which mDNS discovery needs. The port is then on the host: set web_bind_address = "127.0.0.1" as above, or use the nftables rule there.

5. Let DoH name its clients

Behind a proxy, every DoH request arrives from the proxy's address. [server] trusted_proxies lists the peers whose X-Forwarded-For the server believes. It defaults to loopback, which covers a proxy on the same host. A proxy on another host or container has to be listed:

[server]
trusted_proxies = ["127.0.0.0/8", "::1/128", "192.168.1.5/32"]

Otherwise every DoH client is logged as the proxy and gets the proxy's group. See DoH client identity.

Three more effects of the proxy:

  • Login lockout. The lockout counts failures per connecting address. Behind the proxy, every login shares the proxy's count, so a burst of wrong passwords locks everyone out for the lockout window. See Login lockout.
  • Passkeys. A passkey is bound to the name the browser sees, which is now the proxy's. Set [auth.webauthn] rp_id = "dns.example.com" and rp_origin = "https://dns.example.com".
  • /metrics. The proxy also forwards /metrics, which has no authentication. Block it on the proxy if the proxy is reachable from outside the network your Prometheus scrapes from.

All three configurations were run in front of the server with web TLS on and its generated certificate checked, as shipped except for the host name, the public certificate and the server's address. Through each one:

  • HTTP redirected to HTTPS, and the headers above came back;
  • a browser login on a non-loopback name got an HttpOnly; SameSite=Strict; Secure session cookie and loaded the Query Log;
  • the Pi-hole API answered;
  • DoH queries were logged under the real client's address, not the proxy's.

nginx -t and caddy validate accept the files as they are in contrib/.


Renewing certificates

/metrics exports when each certificate the server loaded expires, as ferrousdns_tls_cert_not_after_seconds{listener="web"|"dot"|"doq"}, and the FerrousDnsCertificateExpiring rule fires 21 days ahead. The value is the certificate loaded at start: one renewed on disk counts once the server has restarted.

Dashboard and DoH

The proxy renews its public certificate by itself: Caddy and Traefik through ACME, nginx through certbot's timer. Ferrous DNS does not need a restart for that. Only the self-signed certificate between the proxy and the server has to be replaced, once a year (step 1 above), along with every copy of it: the proxy's, and Prometheus's ca_file.

DoT and DoQ

DoT and DoQ clients see the server's own certificate, so it has to be one they trust. Give it its own files, so that a renewal never replaces the certificate the proxy checks:

[server.encrypted_dns]
dot_enabled = true
tls_cert_path = "/var/lib/ferrous-dns/tls/dot-cert.pem"
tls_key_path  = "/var/lib/ferrous-dns/tls/dot-key.pem"

contrib/certbot/ferrous-dns-deploy-hook.sh runs after the certificate is issued and after every renewal of it. It installs the certificate and the key under the service user, readable by it alone, creates their directory if needed, and restarts the server.

That restart is what moves the dot and doq series forward. certbot renews 30 days before expiry, so FerrousDnsCertificateExpiring firing on a certbot-managed certificate means the renewal or the hook failed.

Keep it out of renewal-hooks/deploy/: certbot runs the hooks there for every certificate on the host, so the renewal of another one, such as the proxy's, would overwrite the DoT files. Pass it with --deploy-hook instead, which certbot keeps with this certificate's renewal settings:

sudo install -m 0755 ferrous-dns-deploy-hook.sh /usr/local/sbin/ferrous-dns-deploy-hook
sudo certbot certonly --standalone -d dns.example.com \
  --deploy-hook /usr/local/sbin/ferrous-dns-deploy-hook

--standalone needs port 80 free. With a proxy holding port 80, use certbot's --webroot or --nginx instead. A resolver reachable only inside your network cannot pass an HTTP challenge: use a DNS challenge plugin for your DNS provider.

For Docker, edit the four settings at the top of the hook:

  • CERT and KEY to the files in the /data directory as the host sees it. docker volume inspect -f '{{ .Mountpoint }}' ferrous-data prints it; Compose names the volume after the project, such as ferrous-dns_ferrous-data;
  • OWNER to 1000:1000;
  • RESTART to docker restart ferrous-dns.
#!/bin/sh
# certbot deploy hook for Ferrous DNS: installs a new or renewed certificate where
# the DoT/DoQ listeners read it, then restarts the server, which loads
# certificates only at startup.
# Guide: https://ferrous-networking.github.io/ferrous-dns/operations/tls-reverse-proxy/
#
#   certbot certonly ... --deploy-hook /usr/local/sbin/ferrous-dns-deploy-hook
#
# Not in renewal-hooks/deploy/: certbot runs those for every certificate, and the
# renewal of another one would overwrite these files. Given with --deploy-hook,
# certbot keeps it with this certificate's renewal settings only.
#
# certbot sets RENEWED_LINEAGE to /etc/letsencrypt/live/<name>. The four
# settings below match contrib/systemd/ferrous-dns.service; override them in the
# environment or edit them here. For Docker, point CERT and KEY at the files in the
# mounted /data directory, set OWNER=1000:1000 and RESTART="docker restart ferrous-dns".
set -eu

CERT=${FERROUS_DNS_CERT:-/var/lib/ferrous-dns/tls/dot-cert.pem}
KEY=${FERROUS_DNS_KEY:-/var/lib/ferrous-dns/tls/dot-key.pem}
OWNER=${FERROUS_DNS_OWNER:-ferrous-dns:ferrous-dns}
RESTART=${FERROUS_DNS_RESTART:-systemctl restart ferrous-dns}

install_private() {
    # Written next to the target and renamed over it, so the server never reads half a file.
    dir=$(dirname "$2")
    if [ ! -d "$dir" ]; then
        install -d -m 0700 "$dir"
        chown "$OWNER" "$dir"
    fi
    install -m 0600 "$1" "$2.new"
    chown "$OWNER" "$2.new"
    mv -f "$2.new" "$2"
}

install_private "$RENEWED_LINEAGE/fullchain.pem" "$CERT"
install_private "$RENEWED_LINEAGE/privkey.pem" "$KEY"
$RESTART

The hook was tested against Pebble, Let's Encrypt's test CA. certbot issued a certificate and then force-renewed it. Each time the hook installed both files with the right owner and mode, and after the restart DoT served the new certificate.