Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nginx SSL/TLS configuration for "A+" Qualys SSL Labs rating
#
# Name: nginx-tls.conf
# Auth: Gavin Lloyd <gavinhungry@gmail.com>
# Desc: Nginx SSL/TLS configuration for "A+" Qualys SSL Labs rating
#
# Enables HTTP/2, PFS, HSTS and OCSP stapling. Configuration options not related
# to SSL/TLS are not included here.
#
# Additional tips:
#
# * Enable CAA DNS record: https://sslmate.com/caa
#
# Example: https://www.ssllabs.com/ssltest/analyze.html?d=gavinhungry.com
#
server {
listen [::]:80;
listen 80;
server_name domain.tld www.domain.tld;
# Redirect all non-https requests
rewrite ^ https://$host$request_uri? permanent;
}
server {
listen [::]:443 ssl http2 default_server;
listen 443 ssl http2 default_server;
server_name domain.tld www.domain.tld;
# Certificate(s) and private key
ssl_certificate /etc/ssl/domain.crt;
ssl_certificate_key /etc/ssl/domain.key;
# RFC-7919 recommended: https://wiki.mozilla.org/Security/Server_Side_TLS#ffdhe4096
ssl_dhparam /etc/ssl/ffdhe4096.pem;
# Or, generate random dhparam
# openssl dhparam 4096 -out /etc/ssl/dhparam.pem
# ssl_dhparam /etc/ssl/dhparam.pem;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ecdh_curve secp521r1:secp384r1;
ssl_ciphers EECDH+AESGCM:EECDH+AES256;
ssl_session_cache shared:TLS:2m;
ssl_buffer_size 4k;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 1.0.0.1 [2606:4700:4700::1111] [2606:4700:4700::1001]; # Cloudflare
# Set HSTS to 365 days
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload' always;
}
@beatquantum
Copy link

beatquantum commented Sep 19, 2021

Hi Gavin, Hi all,

This works for me with nginx 1.21.3 and openssl 1.1.1l - it is in the server block.

server {
...
# Some cybersecurity tools use TLSv1.2 otherwise I would have used only TLSv1.3
ssl_protocols TLSv1.3 TLSv1.2;

    # The TLS 1.2 ciphers below will not work with very old browsers and Android phones.
    # Please do not ignore the +AES256 as otherwise you will get AES128.
    ssl_ciphers ECDH+CHACHA20:ECDH+AESGCM+AES256;

    # The TLS 1.3 ciphers below are fewer than those in the RFC. But they work.
    ssl_conf_command Ciphersuites TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384;

    # The commands below directly affect how Openssl will behave.
    ssl_conf_command Options ServerPreference,PrioritizeChaCha,NoRenegotiation,NoResumptionOnRenegotiation;

    # My server is not heavy loaded so I am okay to use secp384r1. Saw no effect on website speed.
    ssl_ecdh_curve secp521r1:secp384r1;

    # Do not use 2048 (see SSLTEST rating guide)
    ssl_dhparam /etc/ssl/dsa4096.pem;

...
}

@teggsdgreat
Copy link

teggsdgreat commented Nov 12, 2021

Beautiful, it worked

@ray-moncada
Copy link

ray-moncada commented Feb 15, 2022

Do you need to add the header "Content-Security-Policy" for extra security measures?

@Mageek627
Copy link

Mageek627 commented Mar 22, 2022

No need for ssl_dhparam at all

@beatquantum
Copy link

beatquantum commented Mar 23, 2022

Thanks @Mageek627 . You are right. No need for ssl_dhparam. Here is my latest configuration on nginx 1.21.6

ssl_prefer_server_ciphers on;
ssl_ciphers "TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384";
ssl_conf_command Ciphersuites TLS_CHACHA20_POLY1305_SHA256;
ssl_conf_command Options ServerPreference,PrioritizeChaCha,NoRenegotiation,NoResumptionOnRenegotiation;
ssl_ecdh_curve secp521r1:secp384r1;

@stevenlafl
Copy link

stevenlafl commented Jun 10, 2022

Here is one from the NSA/defense.gov, ref https://media.defense.gov/2021/Jan/05/2002560140/-1/-1/0/ELIMINATING_OBSOLETE_TLS_UOO197443-20.PDF & https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-52r2.pdf

ssl_protocols TLSv1.3 TLSv1.2;
ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:AES256-GCM-SHA384;
ssl_ecdh_curve secp384r1;	
ssl_prefer_server_ciphers on;
  
ssl_session_tickets off;

https://github.com/nsacyber/Mitigating-Obsolete-TLS/blob/master/webserver/nginx-tls.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment