TExcelTAD
Home › Linux Server Administration › Terminating TLS in Nginx & Caddy

Terminating TLS in Nginx & Caddy

TLS termination means the reverse proxy handles HTTPS: it holds the certificate and key, does the handshake, and talks plain HTTP to the app on localhost (or re-encrypts to backends on other hosts). One place for certificates, protocol settings and renewals, and the app doesn't need to know about any of it. This lesson configures it in Nginx and Caddy and tests it with curl and openssl s_client, using a private CA made in the session (the previous lesson explains those commands).

Nginx with HTTPS

bash · try it yourself
# a CA and a certificate for app.example.test (see "Certificates, CSRs & a Private CA")
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 365 -subj "/CN=TAD Lab Root CA" 2>/dev/null
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout app.key -out app.csr -subj "/CN=app.example.test" 2>/dev/null
openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -set_serial 7 -days 90 -out app.crt \
  -extfile <(printf 'subjectAltName=DNS:app.example.test\nextendedKeyUsage=serverAuth\n') 2>/dev/null
mkdir -p www && echo "hello from the app" > www/index.html
python3 -m http.server 9000 --bind 127.0.0.1 --directory www > app.log 2>&1 &
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
  access_log off;
  server {                                   # plain HTTP: only redirect
    listen 127.0.0.1:8080;
    server_name app.example.test;
    return 301 https://$host:8443$request_uri;
  }
  server {
    listen 127.0.0.1:8443 ssl;
    server_name app.example.test;
    ssl_certificate     app.crt;             # in production: the full chain
    ssl_certificate_key app.key;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_session_cache   shared:SSL:10m;
    add_header Strict-Transport-Security "max-age=31536000" always;
    location / {
      proxy_pass http://127.0.0.1:9000;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Proto https;
    }
  }
}
EOF
nginx -t -p "$PWD" -c nginx.conf -e stderr 2>&1 | tail -1
for i in $(seq 50); do curl -s 127.0.0.1:9000/ > /dev/null && break; sleep 0.1; done
nginx -p "$PWD" -c nginx.conf -e stderr
R="--resolve app.example.test:8080:127.0.0.1 --resolve app.example.test:8443:127.0.0.1"
echo "--- http is redirected:"
curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' $R http://app.example.test:8080/
echo "--- https with our CA trusted:"
curl -s $R --cacert ca.crt https://app.example.test:8443/
curl -sI $R --cacert ca.crt https://app.example.test:8443/ | grep -i strict | tr -d '\r'
echo "--- https without trusting the CA:"
curl -sS $R https://app.example.test:8443/ 2>&1 | head -1
echo "--- the right IP but the wrong name:"
curl -sS --cacert ca.crt https://127.0.0.1:8443/ 2>&1 | head -1
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null; kill %1
nginx: configuration file /home/learner/nginx.conf test is successful
--- http is redirected:
301 -> https://app.example.test:8443/
--- https with our CA trusted:
hello from the app
Strict-Transport-Security: max-age=31536000
--- https without trusting the CA:
curl: (60) SSL certificate problem: unable to get local issuer certificate
--- the right IP but the wrong name:
curl: (60) SSL: no alternative certificate subject name matches target host name '127.0.0.1'

The two failures are the errors your users would see, in curl's words: untrusted issuer (the client doesn't trust the CA, or the chain is incomplete) and name mismatch (the certificate doesn't list the name the client connected to). --resolve keeps the real name in the URL, so SNI and the name check work while connecting to the test server (Debugging HTTP with curl).

Strict-Transport-Security (HSTS) tells browsers to use HTTPS only for this site for a year. Add it once HTTPS works everywhere on the domain; it can't easily be taken back.

Looking at the handshake with s_client

openssl s_client is the low-level TLS client: it shows the negotiated protocol, cipher and certificate chain, and it can force old protocol versions to prove they are off:

bash · try it yourself
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 365 -subj "/CN=TAD Lab Root CA" 2>/dev/null
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout app.key -out app.csr -subj "/CN=app.example.test" 2>/dev/null
openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -set_serial 7 -days 90 -out app.crt -extfile <(printf 'subjectAltName=DNS:app.example.test\n') 2>/dev/null
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
  access_log off;
  server {
    listen 127.0.0.1:8443 ssl;
    ssl_certificate app.crt;
    ssl_certificate_key app.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    return 200 "ok\n";
  }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
for i in $(seq 50); do curl -sk https://127.0.0.1:8443/ > /dev/null && break; sleep 0.1; done
echo "--- default (best) protocol:"
openssl s_client -connect 127.0.0.1:8443 -servername app.example.test -CAfile ca.crt -brief < /dev/null 2>&1 | grep -E 'Protocol|Ciphersuite|Peer certificate|Verification'
echo "--- forcing TLS 1.2:"
openssl s_client -connect 127.0.0.1:8443 -tls1_2 -CAfile ca.crt -brief < /dev/null 2>&1 | grep -E 'Protocol|Ciphersuite'
echo "--- forcing TLS 1.1 (the client's own security level lowered, so only the server can refuse):"
openssl s_client -connect 127.0.0.1:8443 -tls1_1 -cipher 'DEFAULT:@SECLEVEL=0' < /dev/null > /dev/null 2>&1 && echo "TLS 1.1 accepted!" || echo "TLS 1.1 refused"
echo "--- the chain the server sends:"
openssl s_client -connect 127.0.0.1:8443 -servername app.example.test -showcerts < /dev/null 2>/dev/null | grep -E '^ *[0-9] s:|^ *i:'
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
--- default (best) protocol:
Protocol version: TLSv1.3
Ciphersuite: TLS_AES_256_GCM_SHA384
Peer certificate: CN = app.example.test
Verification: OK
--- forcing TLS 1.2:
Protocol version: TLSv1.2
Ciphersuite: ECDHE-ECDSA-AES256-GCM-SHA384
--- forcing TLS 1.1 (the client's own security level lowered, so only the server can refuse):
TLS 1.1 refused
--- the chain the server sends:
 0 s:CN = app.example.test
   i:CN = TAD Lab Root CA

The chain section is the check for the "missing intermediate" problem: on a public site you should see depth 0 (your certificate) and 1 (the intermediate), and the last issuer should be a root your clients trust. -servername sets SNI; without it, servers with several certificates on one IP return the default one, which is a common cause of "wrong certificate" reports from old clients.

The same in Caddy

With a public name, Caddy needs no TLS configuration at all (Caddy as a Reverse Proxy). For your own certificates, the tls directive takes the files:

bash · try it yourself
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 365 -subj "/CN=TAD Lab Root CA" 2>/dev/null
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout app.key -out app.csr -subj "/CN=app.example.test" 2>/dev/null
openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -set_serial 7 -days 90 -out app.crt -extfile <(printf 'subjectAltName=DNS:app.example.test\n') 2>/dev/null
cat > Caddyfile <<'EOF'
{
	auto_https disable_redirects
	https_port 8443
}
app.example.test:8443 {
	tls app.crt app.key
	respond "hello over TLS from Caddy"
}
EOF
caddy validate --config Caddyfile 2>&1 | tail -1
caddy start --config Caddyfile > caddy.log 2>&1 && echo "caddy started"
for i in $(seq 50); do curl -sk https://127.0.0.1:8443/ > /dev/null && break; sleep 0.1; done
curl -s --resolve app.example.test:8443:127.0.0.1 --cacert ca.crt https://app.example.test:8443/; echo
curl -s -o /dev/null -w '%{http_version}\n' --resolve app.example.test:8443:127.0.0.1 --cacert ca.crt https://app.example.test:8443/
caddy stop > /dev/null 2>&1 && echo "caddy stopped"
Valid configuration
caddy started
hello over TLS from Caddy
2
caddy stopped

Caddy negotiated HTTP/2 without being asked; it also offers HTTP/3 over UDP on the same port.

Settings worth having

Setting Recommendation
protocols TLS 1.2 and 1.3 only (both servers' defaults on current versions)
ciphers leave the server's modern defaults; for an explicit policy, use Mozilla's SSL Configuration Generator (ssl-config.mozilla.org) "intermediate" profile
certificate file the full chain
key file 600, owned by root (Nginx reads it before dropping privileges)
OCSP stapling less important since Let's Encrypt ended OCSP in 2025; harmless where your CA still supports it
HSTS once all of the site works over HTTPS
redirects port 80 only redirects to HTTPS (and serves ACME HTTP-01 challenges)

Test yourself

curl https://app.example.test fails with unable to get local issuer certificate, while browsers work. What is the most likely cause?

  1. The key is wrong
  2. The server doesn't send the intermediate certificate; browsers may fill the gap, curl doesn't
  3. TLS 1.3 is disabled
  4. The DNS name is wrong

What does -servername do in openssl s_client?

  1. Sets the HTTP Host header
  2. Sends SNI, so a server with several certificates on one address returns the right one
  3. Verifies the certificate
  4. Selects the cipher

Why add HSTS only once HTTPS works for the whole site?

  1. It slows down HTTP
  2. Browsers then refuse plain HTTP for the domain for max-age, so any part that only works over HTTP breaks and can't be quickly undone
  3. It disables TLS 1.2
  4. It changes the certificate