Skip to content

Nginx TLS hardening

Security · Nginx
Studied · 2026-04-15

Problem

Nginx was set up from a two-year-old tutorial: TLS 1.0 and 1.1 are enabled, the cipher suite is the default, there are no security headers (HSTS, X-Frame-Options). SSL Labs gives it a B. Nobody has broken in yet — so nobody looks.

Solution

A minimal hardening set: TLS 1.2/1.3 only, modern ciphers as in the Mozilla SSL Configuration Generator "Intermediate" profile, HSTS, basic security headers. Put the config in a separate tls.conf file and include it in every server block — a single source of truth.

Example

/etc/nginx/conf.d/tls.conf:

# Current TLS versions only
ssl_protocols TLSv1.2 TLSv1.3;

# Modern cipher suite (Mozilla "intermediate")
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;
ssl_prefer_server_ciphers off;

# Sessions — faster handshakes
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;

# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 8.8.8.8 valid=300s;

# Security headers
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

Using it in a server block:

server {
    listen 443 ssl http2;
    server_name app.example.com;

    ssl_certificate     /etc/ssl/app.crt;
    ssl_certificate_key /etc/ssl/app.key;

    include /etc/nginx/conf.d/tls.conf;

    location / {
        proxy_pass http://backend;
    }
}

Check after a reload:

nginx -t && systemctl reload nginx

# SSL Labs-style check from the CLI (testssl.sh)
docker run --rm -ti drwetter/testssl.sh https://app.example.com

Target grade — A+ on ssllabs.com.

When to use

  • Any internet-facing Nginx.
  • Internal services — optional, but don't enable HSTS on dev domains (it will block plain http access).
  • Mandatory before handing over to a pentest, so the budget isn't spent on "we found you have TLS 1.0".

Source

How to apply it yourself

  1. Move the current ssl_* directives out of the server blocks into conf.d/tls.conf.
  2. Bring it in line with Mozilla Intermediate + TLS 1.2/1.3.
  3. Add HSTS only after confirming that all traffic goes over https.
  4. Run testssl.sh — get an A+.
  5. Once the grade is A+ and the config is rolled out to every Nginx — status Studied.