Security and hardening · Intermediate · 22 min
Configure modern TLS on Nginx and check it with testssl.sh
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Nginx 1.30 (nginx.org) · OpenSSL 3.5 · testssl.sh 3.2.5
You will tighten an Nginx 1.30 HTTPS site to the Mozilla intermediate TLS profile with ECDSA and RSA certificates, session resumption and a staged HSTS header, and prove each change with testssl.sh 3.2.5.
On this page
Turning on HTTPS is one decision. Choosing what that HTTPS will accept is another, and most servers never make it. Out of the box, Nginx 1.30 offers TLS 1.2 and 1.3, which is good, but for TLS 1.2 it uses the OpenSSL group HIGH:!aNULL:!MD5. That group still includes old CBC-mode ciphers and suites where the browser encrypts the session key with the server's RSA key. Neither is broken on the day you deploy, but both are weaker than what every current browser supports, and RSA key exchange has no forward secrecy: anyone who records your traffic today and steals the key later can read it.
Mozilla publishes three server profiles, and two matter today:
- Intermediate: TLS 1.2 and 1.3, only ECDHE key exchange and only AEAD ciphers (AES-GCM and ChaCha20-Poly1305). It still serves phones as old as Android 4.4.2. This is the right default for a public site.
- Modern: TLS 1.3 only, ECDSA certificates only. Simpler and stronger, but it shuts out Android 9 and older, which still turn up on budget phones in many markets, including across Africa. Use it for APIs and internal tools where you control the clients.
In this tutorial you build a small lab site on port 8443 with a local certificate authority, scan it with testssl.sh, fix what it reports, then apply the same configuration to your live site. Every change gets a check you can see. It assumes Nginx 1.30 from nginx.org, as in /tutorials/install-nginx-on-ubuntu-26-04.
Step 1 · Install the tools
You need OpenSSL's command-line tool, Git to fetch testssl.sh, jq to read its JSON report, and two small packages testssl.sh relies on: hexdump (in bsdextrautils) and dig (in bind9-dnsutils).
sudo apt update
sudo apt install -y openssl git jq bsdextrautils bind9-dnsutilstestssl.sh is a single Bash script with no build step. Clone the 3.2.5 release tag into /opt:
sudo git clone --depth 1 --branch v3.2.5 https://github.com/testssl/testssl.sh.git /opt/testssl.sh
/opt/testssl.sh/testssl.sh --version | grep 'testssl.sh'A copy installed this way gets no updates from apt. testssl.sh changes often as new TLS features and attacks appear, so check its releases page every few months. To move to a newer release, delete the folder and clone the new tag, for example sudo rm -rf /opt/testssl.sh and then the same git clone with --branch v3.2.6.
Check it worked
The second command prints a line containing testssl.sh and 3.2.5, and openssl version prints OpenSSL 3.5.x, the version Ubuntu 26.04 ships.
openssl versionStep 2 · Make a local CA with an ECDSA and an RSA certificate
A browser can use either of two kinds of certificate key. ECDSA keys are small and fast to sign with; a P-256 key gives security comparable to a 3072-bit RSA key. RSA keys are older and still needed by a few clients. Nginx can hold one of each for the same site and hands each client the one it supports, so you do not have to choose.
For the lab, you sign both with your own throwaway certificate authority. Everything lives in ~/tls-demo. The key files are created inside a subshell with umask 077, so they are private from the moment they exist and your shell's umask is left alone.
mkdir -p ~/tls-demo
cd ~/tls-demo
(
umask 077
openssl req -x509 -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \
-days 30 -subj "/CN=TLS demo local CA" -keyout ca.key -out ca.pem
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \
-subj "/CN=localhost" -keyout ecdsa.key -out ecdsa.csr
openssl req -new -newkey rsa:2048 -nodes \
-subj "/CN=localhost" -keyout rsa.key -out rsa.csr
)Now the extensions every server certificate needs. The key usage is only digitalSignature, for the RSA certificate too: once RSA key exchange is gone, the RSA key only ever signs, it never decrypts a session key. The CRL address is what public certificates carry for revocation; this one is never fetched in the lab.
cd ~/tls-demo
cat > leaf.ext <<'EXT'
basicConstraints = critical, CA:FALSE
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
subjectAltName = DNS:localhost, IP:127.0.0.1
crlDistributionPoints = URI:http://localhost/tls-demo-ca.crl
EXT
openssl x509 -req -in ecdsa.csr -CA ca.pem -CAkey ca.key -CAcreateserial -days 7 -extfile leaf.ext -out ecdsa.crt
openssl x509 -req -in rsa.csr -CA ca.pem -CAkey ca.key -CAcreateserial -days 7 -extfile leaf.ext -out rsa.crtCopy the certificates and keys where Nginx reads them. The keys stay readable by root only; the Nginx master process runs as root and loads them before it starts its workers.
cd ~/tls-demo
sudo install -d -m 755 /etc/nginx/tls-lab
sudo install -m 644 ecdsa.crt rsa.crt /etc/nginx/tls-lab/
sudo install -m 600 ecdsa.key rsa.key /etc/nginx/tls-lab/Check it worked
Both certificates verify against your CA, and the key types differ:
cd ~/tls-demo
openssl verify -CAfile ca.pem ecdsa.crt rsa.crt
openssl x509 -in ecdsa.crt -noout -text | grep 'Public Key Algorithm'
openssl x509 -in rsa.crt -noout -text | grep 'Public Key Algorithm'The first command prints ecdsa.crt: OK and rsa.crt: OK. The next two print id-ecPublicKey and rsaEncryption.
Step 3 · Serve a lab site with the Nginx defaults
Start with a server block that sets only the certificates, so you can see what the defaults offer. It listens on port 8443, which leaves your live sites alone. The two ssl_certificate pairs are how Nginx holds an ECDSA and an RSA certificate at once.
sudo tee /etc/nginx/conf.d/tls-lab.conf > /dev/null <<'CONF'
server {
listen 8443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/tls-lab/ecdsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/ecdsa.key;
ssl_certificate /etc/nginx/tls-lab/rsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/rsa.key;
root /usr/share/nginx/html;
}
CONF
sudo nginx -tsudo systemctl reload nginxCheck it worked
Ask for a TLS 1.2 connection with an ECDSA suite, then with an RSA suite, and look at the certificate the server sends each time:
cd ~/tls-demo
openssl s_client -connect 127.0.0.1:8443 -servername localhost -CAfile ca.pem \
-tls1_2 -cipher ECDHE-ECDSA-AES128-GCM-SHA256 </dev/null 2>/dev/null \
| openssl x509 -noout -text | grep 'Public Key Algorithm'
openssl s_client -connect 127.0.0.1:8443 -servername localhost -CAfile ca.pem \
-tls1_2 -cipher ECDHE-RSA-AES128-GCM-SHA256 </dev/null 2>/dev/null \
| openssl x509 -noout -text | grep 'Public Key Algorithm'The first prints id-ecPublicKey, the second rsaEncryption: one site, two certificates. Now try two suites the defaults should not offer, one with CBC mode and one with RSA key exchange:
for c in AES256-SHA256 AES256-GCM-SHA384; do
if openssl s_client -connect 127.0.0.1:8443 -servername localhost -tls1_2 -cipher "$c" </dev/null >/dev/null 2>&1; then
echo "$c: accepted"
else
echo "$c: refused"
fi
doneWith the defaults, both lines say accepted. That is what the next step makes testssl.sh report.
Step 4 · Scan the defaults with testssl.sh
testssl.sh connects to a server many times with chosen protocols, ciphers and options, and reports what the server accepts. A full run with no options also tests for named vulnerabilities and takes several minutes. Here you choose the sections that matter for configuration: protocols (-p), cipher categories (-s), forward secrecy (-f), server defaults such as certificates and session resumption (-S) and HTTP headers (-h).
The other options: --add-ca trusts your lab CA for the chain check, --ip one tests the first address only (localhost has an IPv6 address too, and the lab only listens on IPv4), --warnings batch stops it pausing for a keypress, and --jsonfile writes every finding to a file you can query. testssl.sh exits with a non-zero code when a test was ambiguous, so the || part prints that code instead of letting it look like a crash.
cd ~/tls-demo
/opt/testssl.sh/testssl.sh --quiet --warnings batch --ip one --add-ca ca.pem \
-p -s -f -S -h --jsonfile baseline.json --overwrite https://localhost:8443/ \
|| echo "testssl.sh finished with exit code $?"The screen output is long and colour-coded: green is good, yellow and red need a look. Each finding in the JSON file has an id, a severity (OK, INFO, LOW, MEDIUM, HIGH or CRITICAL) and a finding. List everything rated LOW or worse:
cd ~/tls-demo
jq -r '.[] | select(.severity | IN("LOW","MEDIUM","HIGH","CRITICAL")) | "\(.severity)\t\(.id)\t\(.finding)"' baseline.jsonCheck it worked
The list includes at least these two lines (the tab spacing may differ):
LOW cipherlist_OBSOLETED offered: the server accepts CBC ciphers.LOW HSTS not offered: there is no Strict-Transport-Security header.
Confirm both directly:
cd ~/tls-demo
jq -r '.[] | select(.id=="cipherlist_OBSOLETED" or .id=="HSTS") | "\(.id): \(.finding)"' baseline.json | tee baseline-check.txt
grep -x 'cipherlist_OBSOLETED: offered' baseline-check.txt
grep -x 'HSTS: not offered' baseline-check.txttestssl.sh rates RSA key exchange with AES-GCM as acceptable (cipherlist_STRONG_NOFS), but Mozilla's intermediate profile removes it for the forward-secrecy reason in the introduction. A scanner's grade is a floor, not your target.
Step 5 · Apply the Mozilla intermediate profile
Put the TLS settings in one snippet, so every HTTPS site on the server can include the same file. This is the intermediate profile from Mozilla's guidelines version 6.0, written for Nginx 1.30 and OpenSSL 3.5:
sudo mkdir -p /etc/nginx/snippets
sudo tee /etc/nginx/snippets/tls-intermediate.conf > /dev/null <<'CONF'
# TLS 1.0 and 1.1 are off; these are the only versions offered.
ssl_protocols TLSv1.2 TLSv1.3;
# TLS 1.2 suites: ECDHE key exchange (forward secrecy) and AEAD ciphers only.
# TLS 1.3 suites are not set here: OpenSSL's defaults are already the three AEAD suites.
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
# Every suite left is strong, so let the client pick the one that is fastest on its hardware
# (phones without AES instructions pick ChaCha20).
ssl_prefer_server_ciphers off;
# Key-exchange groups, best first. X25519MLKEM768 is a hybrid post-quantum group
# that OpenSSL 3.5 and current browsers support.
ssl_ecdh_curve X25519MLKEM768:X25519:prime256v1:secp384r1;
# Session resumption: a returning client skips the full handshake.
ssl_session_cache shared:TLS:10m;
ssl_session_timeout 1d;
CONFA note on the lines you do not see. There is no ssl_dhparam, because no DHE suite is left that would use it. There is no ssl_session_tickets off. Older guides switch tickets off because Nginx used to keep one ticket key for as long as it ran, which weakens forward secrecy. Since Nginx 1.23.2, a shared ssl_session_cache also makes Nginx create and rotate the ticket keys itself, so tickets can stay on with a single server. If several servers sit behind one load balancer, each has its own keys; either accept that resumption only works when a client returns to the same server, or turn tickets off.
Resumption matters more than it looks on mobile. A returning visitor on a slow 3G link or a prepaid data bundle saves a round trip and a few kilobytes on every new connection.
Now rewrite the lab server block to include the snippet and send a first, short HSTS header:
sudo tee /etc/nginx/conf.d/tls-lab.conf > /dev/null <<'CONF'
server {
listen 8443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/tls-lab/ecdsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/ecdsa.key;
ssl_certificate /etc/nginx/tls-lab/rsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/rsa.key;
include /etc/nginx/snippets/tls-intermediate.conf;
add_header Strict-Transport-Security "max-age=300" always;
root /usr/share/nginx/html;
}
CONF
sudo nginx -tsudo systemctl reload nginxHSTS tells browsers to use only HTTPS for your site for max-age seconds. Five minutes is a safe first value, because a mistake wears off quickly. The full roll-out, and why add_header in a location can silently drop this header, is in /tutorials/add-security-headers-in-nginx.
Check it worked
Run the same two probes as before:
for c in AES256-SHA256 AES256-GCM-SHA384; do
if openssl s_client -connect 127.0.0.1:8443 -servername localhost -tls1_2 -cipher "$c" </dev/null >/dev/null 2>&1; then
echo "$c: accepted"; false
else
echo "$c: refused"
fi
doneBoth now say refused. A modern client still gets TLS 1.3 with the hybrid post-quantum group:
cd ~/tls-demo
openssl s_client -connect 127.0.0.1:8443 -servername localhost -CAfile ca.pem \
-tls1_3 -groups X25519MLKEM768 -brief </dev/null 2>&1 | tee tls13.txt
grep 'Protocol version: TLSv1.3' tls13.txtThe output shows CONNECTION ESTABLISHED, Protocol version: TLSv1.3, a Ciphersuite line, Verification: OK and something like Negotiated TLS1.3 group: X25519MLKEM768.
Step 6 · Scan again and read what is left
Run testssl.sh with the same options and a new report file:
cd ~/tls-demo
/opt/testssl.sh/testssl.sh --quiet --warnings batch --ip one --add-ca ca.pem \
-p -s -f -S -h --jsonfile intermediate.json --overwrite https://localhost:8443/ \
|| echo "testssl.sh finished with exit code $?"
jq -r '.[] | select(.severity | IN("LOW","MEDIUM","HIGH","CRITICAL")) | "\(.severity)\t\(.id)\t\(.finding)"' intermediate.jsonThe CBC finding is gone. One new item appears, MEDIUM HSTS_time max-age too short: testssl.sh wants at least 180 days. That is the warning you expect during a staged roll-out, not a fault. Read anything else on the list before you dismiss it; on a lab site with a private CA, findings about certificate transparency or a short certificate life are expected.
Look at the session ticket line too:
cd ~/tls-demo
jq -r '.[] | select(.id=="TLS_session_ticket") | .finding' intermediate.jsonIt prints something like valid for 86400 seconds only (<daily), the ssl_session_timeout of one day you set.
Check it worked
cd ~/tls-demo
jq -r '.[] | select(.id=="cipherlist_OBSOLETED" or .id=="HSTS_time") | "\(.id): \(.severity) \(.finding)"' intermediate.json | tee intermediate-check.txt
grep -x 'cipherlist_OBSOLETED: INFO not offered' intermediate-check.txt
grep 'HSTS_time: MEDIUM max-age too short' intermediate-check.txtcipherlist_OBSOLETED is now INFO not offered, and the HSTS warning is the short max-age.
Step 7 · Raise HSTS to its final value
On your live site you raise max-age in stages over a few weeks: 300 seconds, then a day (86400), then a month (2592000), then Mozilla's recommended two years (63072000). Move to the next stage only when every page and every subdomain you care about has worked over HTTPS for the whole of the previous one. Once a browser has seen a long value, lowering it on the server does not help visitors who saw the old header until it runs out. Add includeSubDomains only when every subdomain serves HTTPS.
The lab has no visitors, so jump straight to the final value to see how testssl.sh grades it:
sudo tee /etc/nginx/conf.d/tls-lab.conf > /dev/null <<'CONF'
server {
listen 8443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/tls-lab/ecdsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/ecdsa.key;
ssl_certificate /etc/nginx/tls-lab/rsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/rsa.key;
include /etc/nginx/snippets/tls-intermediate.conf;
add_header Strict-Transport-Security "max-age=63072000" always;
root /usr/share/nginx/html;
}
CONF
sudo nginx -tsudo systemctl reload nginxOnly the headers changed, so scan only the headers section:
cd ~/tls-demo
/opt/testssl.sh/testssl.sh --quiet --warnings batch --ip one --add-ca ca.pem \
-h --jsonfile hsts.json --overwrite https://localhost:8443/ \
|| echo "testssl.sh finished with exit code $?"Check it worked
cd ~/tls-demo
jq -r '.[] | select(.id=="HSTS_time") | "\(.severity) \(.finding)"' hsts.json | tee hsts-check.txt
grep '^OK 730 days' hsts-check.txtThe line reads OK 730 days (=63072000 seconds) > ....
Step 8 · Try the modern profile on a second port
If every client of a service is recent, for example your own mobile app on Android 10 or later, or an internal API, the modern profile is shorter. It needs no cipher list at all: TLS 1.3 only allows AEAD suites. It also drops the RSA certificate, because every TLS 1.3 client supports ECDSA.
sudo tee /etc/nginx/snippets/tls-modern.conf > /dev/null <<'CONF'
ssl_protocols TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ecdh_curve X25519MLKEM768:X25519:prime256v1:secp384r1;
ssl_session_cache shared:TLS:10m;
ssl_session_timeout 1d;
CONF
sudo tee /etc/nginx/conf.d/tls-lab-modern.conf > /dev/null <<'CONF'
server {
listen 9443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/tls-lab/ecdsa.crt;
ssl_certificate_key /etc/nginx/tls-lab/ecdsa.key;
include /etc/nginx/snippets/tls-modern.conf;
add_header Strict-Transport-Security "max-age=63072000" always;
root /usr/share/nginx/html;
}
CONF
sudo nginx -tThe session cache line names the same zone, TLS, with the same size as the intermediate snippet, so both sites share it.
sudo systemctl reload nginxCheck it worked
TLS 1.3 connects on port 9443 and TLS 1.2 does not:
cd ~/tls-demo
openssl s_client -connect 127.0.0.1:9443 -servername localhost -CAfile ca.pem -tls1_3 -brief </dev/null 2>&1 | grep 'Protocol version: TLSv1.3'
if openssl s_client -connect 127.0.0.1:9443 -servername localhost -tls1_2 </dev/null >/dev/null 2>&1; then
echo "TLS 1.2: accepted"; false
else
echo "TLS 1.2: refused"
fiYou see Protocol version: TLSv1.3 and then TLS 1.2: refused. Keep the intermediate profile for public websites.
Step 9 · Apply it to your live site and remove the lab
In the HTTPS server block of your live site, add the include and the HSTS line at its first stage, next to the certificate lines Certbot wrote (/tutorials/free-https-with-lets-encrypt-and-nginx):
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/nginx/snippets/tls-intermediate.conf;
add_header Strict-Transport-Security "max-age=300" always;
}Certbot's Nginx plugin may also have added include /etc/letsencrypt/options-ssl-nginx.conf;. That file sets its own ssl_protocols, ssl_ciphers and session cache, and duplicate settings either make nginx -t fail or let one of the two silently win. Keep one source of TLS settings: comment out Certbot's include when you add yours, then run sudo nginx -t.
To serve an ECDSA and an RSA certificate from Let's Encrypt, request a second certificate with a different name and key type, for example sudo certbot certonly --nginx --key-type rsa --cert-name example.com-rsa -d example.com, and add its two lines below the first pair. Certbot's current default key type is ECDSA.
OCSP stapling and Let's Encrypt
Older guides tell you to add ssl_stapling on;. Stapling meant the server fetched a signed "not revoked" answer from the certificate authority's OCSP service and sent it to the browser. Let's Encrypt stopped putting OCSP addresses in its certificates on 7 May 2025 and switched its OCSP service off on 6 August 2025; revocation now works through certificate revocation lists (CRLs) only. With a Let's Encrypt certificate there is nothing to staple, and Nginx logs a warning like "ssl_stapling" ignored, no OCSP responder URL in the certificate if the line is present. Leave stapling off, which is the Nginx default. If your certificate comes from another authority whose certificates still list an OCSP address, stapling still helps. testssl.sh shows which you have: cert_ocspURL and cert_crlDistributionPoints in the report.
Reload and scan your public site. testssl.sh also runs on macOS and in WSL, so you can run it from your own computer, which tests what visitors see, including any firewall or proxy in front of the server. Replace example.com with your domain:
sudo nginx -t
sudo systemctl reload nginx
/opt/testssl.sh/testssl.sh --quiet --warnings batch -p -s -f -S -h --jsonfile ~/tls-demo/live.json --overwrite https://example.com/Now remove the lab. Check the paths before you run this: it deletes the lab's server blocks, its certificates and ~/tls-demo, and keeps the two snippets your live site now includes.
sudo rm -f /etc/nginx/conf.d/tls-lab.conf /etc/nginx/conf.d/tls-lab-modern.conf
sudo rm -rf /etc/nginx/tls-lab
rm -rf ~/tls-demo
sudo nginx -tsudo systemctl reload nginxCheck it worked
On your live site the scan shows cipherlist_OBSOLETED as not offered, TLS 1.2 and 1.3 offered and nothing older, and HSTS_time warning about a short max-age until you reach the later stages. After the lab is removed, nothing listens on the lab ports:
if openssl s_client -connect 127.0.0.1:8443 </dev/null >/dev/null 2>&1; then
echo "port 8443 still answers"; false
else
echo "lab removed"
fiIt prints lab removed.
Next steps
- Get a real certificate for the site:
/tutorials/free-https-with-lets-encrypt-and-nginx. - Add the rest of the security headers, and the HSTS roll-out in full:
/tutorials/add-security-headers-in-nginx. - Audit the whole server, not only its TLS:
/tutorials/audit-a-server-with-lynis. - Put a monthly reminder in your calendar to scan the site again and check for a new testssl.sh release; TLS advice changes slowly but it does change.
Related tutorials
- Write custom Fail2ban jails and filters for your own appsYou will have Fail2ban 1.1 banning repeated failed sign-ins to your own app and to an nginx basic-auth area, with filters you tested against sample logs, your own addresses allow-listed, and longer bans for repeat offenders.
- Validate file uploads before you store or serve themYou will take a FastAPI image upload route that trusts everything the browser sends, prove its flaws with pytest, and rebuild it with size limits, type checks from the file's own bytes, Pillow re-encoding, random storage names and safe download headers.
- Use SSH certificates instead of copying keys to every serverYou will run a small SSH certificate authority with ssh-keygen, log in to an Ubuntu 26.04 server with an eight-hour user certificate, give the server a host certificate so clients stop asking about fingerprints, and revoke a certificate the server then refuses.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials