tuxvador@blog:~/blog/internal-tls-reverse-proxy-backends$

~/blog/internal-tls-reverse-proxy-backends

TLS Ends at the Reverse Proxy. Then What?

4 min readLire en français →

#security#nginx#tls#homelab#self-hosting

The usual homelab setup, mine included: one nginx reverse proxy gets Let’s Encrypt certificates for every public name, and forwards each request to the right container with proxy_pass http://10.99.99.x:port. The browser shows a padlock. The cleartext sensor showed what happened after the proxy: a push-app login, a media-server token and two session cookies, all crossing in plain HTTP. One of those hops went across the home LAN to a laptop.

This post covers closing every one of those hops so that the proxy encrypts and verifies the backend, without buying anything or exposing internal names.

Encrypting isn’t enough

proxy_pass https:// on its own encrypts the hop and verifies nothing: nginx’s default is proxy_ssl_verify off. Anyone who can sit in the middle can present any certificate. To be worth doing, each hop needs:

  1. a certificate on the backend,
  2. a CA the proxy trusts for it,
  3. proxy_ssl_verify on and the name to check.

Public certificates for internal backends are awkward (DNS challenges for names that shouldn’t be public, and certificate transparency logs listing them). Self-signed certificates pinned one by one work, but a dozen pins is a dozen things to rotate. So: one internal CA.

A CA that can’t sign for the internet

The worry with a home CA is the key. If it leaks, whoever holds it can mint a certificate for any site, and any machine that trusts the CA will believe them. Name constraints limit that. An X.509 CA with a nameConstraints extension can only issue certificates for the names and addresses it lists, and verifiers reject anything else.

[ v3_ca ]
basicConstraints = critical, CA:TRUE, pathlen:0
keyUsage         = critical, keyCertSign, cRLSign
nameConstraints  = critical, permitted;DNS:homelab.internal, \
                   permitted;IP:10.0.0.0/255.0.0.0, \
                   permitted;IP:172.16.0.0/255.240.0.0, \
                   permitted;IP:192.168.0.0/255.255.0.0

The CA is an EC P-256 key on the monitoring container, root-only. A small wrapper issues leaf certificates with a SAN of <name>.homelab.internal plus the backend’s IP, valid five years:

internal-ca issue ntfy 10.99.99.10
internal-ca issue jellyfin 10.99.99.13

Even if this key leaked, it couldn’t sign a certificate for a bank or for my own public domains.

The proxy side

The proxy trusts only that CA for upstreams. Each location gets the same five lines:

location / {
    proxy_pass https://10.99.99.10:2586;
    proxy_ssl_verify              on;
    proxy_ssl_trusted_certificate /etc/nginx/upstream-certs/homelab-internal-ca.pem;
    proxy_ssl_name                ntfy.homelab.internal;
    proxy_ssl_server_name         on;
    proxy_ssl_verify_depth        2;
    include snippets/proxy-headers.conf;
}

proxy_ssl_name is what makes verification useful. The certificate must name exactly that backend, so a valid certificate for the wrong service fails too. I tested each hop both ways with openssl s_client -verify_hostname: the right name gives 0 (ok), a wrong one gives 62 (hostname mismatch).

The backend side: every app is different

That was most of the work. Each backend had its own way to serve TLS, and a few couldn’t:

Backend How it serves TLS
ntfy native listen-https on the LAN address; plain HTTP kept on 127.0.0.1 only, for Grafana’s local webhook
homelab dashboard (uvicorn) --ssl-certfile / --ssl-keyfile
Jellyfin its own HTTPS port (8920) with a PKCS#12 file; plain 8096 kept for the TV app on the LAN
File Browser --cert / --key in a systemd drop-in
Headscale + its web UI no good native option: a small nginx on the VPN container terminates TLS and forwards to 127.0.0.1
apps on the laptop the same: a local nginx in front, port = app port + 10000, the app ports firewalled to the proxy only
Grafana already HTTPS with a self-signed cert: pinned rather than reissued
Uptime Kuma serves a copy of the proxy’s own Let’s Encrypt certificate, verified against the public CAs, re-synced daily by a timer

The “small nginx in front” pattern is worth knowing. When an app can’t do TLS, or does it badly, a local nginx bound to the LAN address can do it, while the app listens only on loopback. The app doesn’t change, and its plaintext never leaves the machine.

The push server had one more client to update. Wazuh posts its alerts to ntfy over HTTP with a bearer token. That was a cleartext token too. Its integration script now posts to https:// and verifies with the same CA file.

The proxy header trap

One more hop was leaking, though not a secret this time. It leaked context. On two app vhosts, a location block set proxy_set_header Connection ""; for keep-alive. In nginx, any proxy_set_header in a location drops every one inherited from the server block. So the app stopped receiving X-Forwarded-Proto, X-Forwarded-For and X-Real-IP. It thought every request was plain HTTP from the proxy’s address, and set its session cookies without Secure.

The fix is to repeat all five headers in every block that sets any. Better, keep them in one snippet and include it in every location. The same rule applies to add_header, which is why the security headers live in a snippet too.

Checking that it’s done

Two commands on the proxy tell you whether anything is left:

nginx -T | grep -E 'proxy_pass http://'       # plaintext upstreams
nginx -T | grep -E 'proxy_ssl_verify\s+off'   # encrypted but unverified

Both print nothing now. The sensor has reported no new cleartext hop since. The last plaintext Jellyfin token it logged is timestamped thirty seconds before that hop was switched.

Pitfalls

  • proxy_pass https:// verifies nothing by default. Without proxy_ssl_verify on it encrypts the hop and accepts any certificate.
  • Set proxy_ssl_name. Without it, a valid certificate for the wrong backend passes.
  • Name-constrain a home CA. It costs one line and caps the damage of a leaked key.
  • Check the file nginx actually loads. My helper edited sites-available/, but for two vhosts sites-enabled/ held a regular file, not a link. nginx -T caught it before the switch.
  • A location-level proxy_set_header drops the server-level ones, and with them the app’s idea of HTTPS and of the client IP.
  • Keep plain ports where something still needs them, and know which ones. Jellyfin’s 8096 stays for the TV. That leg is still cleartext, and the sensor will say so if it carries a token.
  • Five-year leaves still expire. The renewal date is in the runbook, and a calendar reminder sits a month before it.

cd ~/blog