Nginx TLS 1.3 Configuration Guide and Cipher Suite Optimization: Achieve A+ on SSL Labs

Security tutorial - IT technology blog
Security tutorial - IT technology blog

Introduction: Why are TLS 1.3 and Cipher Suites important?

In the digital age, website security is a mandatory requirement, no longer an option. Without HTTPS (a secure protocol based on TLS/SSL), browsers will warn that your website is “Not Secure”. This negatively impacts user experience and reduces credibility. HTTPS encrypts all data exchanged between the browser and the server, protecting it from eavesdropping or tampering.

For Nginx – a powerful, popular web server – effective HTTPS configuration is extremely important. However, not all HTTPS configurations provide optimal security. We need to delve deeper into TLS versions and select appropriate cipher suites. This helps your website not only be secure but also operate smoothly and quickly.

This article guides you through configuring Nginx to use TLS 1.3 – the latest TLS version with significant security and performance improvements. At the same time, we will optimize the cipher suites. The ultimate goal? Your website will achieve an absolute A+ rating on the SSL Labs testing tool – a reputable benchmark for TLS/SSL security configuration.

Core Concepts: Master Before You Practice

What is TLS and why is TLS 1.3 superior?

TLS (Transport Layer Security) is a protocol for encrypting data over a network, replacing the outdated SSL (Secure Sockets Layer). This protocol establishes a secure communication channel between two parties (e.g., browser and server) through a handshake process, authentication, and data encryption. The main purpose of TLS is to ensure three core elements:

  • Encryption: Data cannot be read by third parties.
  • Authentication: Ensures you are talking to the correct server and the server is talking to the correct you.
  • Data Integrity: Data is not altered during transmission.

TLS 1.3 is the latest version, offering significant improvements over TLS 1.2:

  • Higher Security: Completely removes weak cipher suites and less secure features from TLS 1.2. All information during the handshake process is encrypted, significantly reducing the risk of eavesdropping.
  • Better Performance: Shortens the handshake process from 2 round-trips to just 1 round-trip (1-RTT). This version also supports 0-RTT (zero round-trip time) for established connections, helping to reduce latency and increase page load speed.
  • Forward Secrecy Only: TLS 1.3 exclusively uses ephemeral keys. This ensures that even if the server’s private key is compromised later, previously encrypted data remains absolutely secure.

What is a Cipher Suite?

A Cipher Suite is a set of algorithms used to secure network connections via TLS/SSL. Each cipher suite includes the following components:

  • Key Exchange Algorithm: Determines how the client and server securely exchange encryption keys (e.g., ECDHE, DHE).
  • Authentication Algorithm: Verifies the identity of the server (and optionally the client) (e.g., RSA, ECDSA).
  • Symmetric Encryption Algorithm: Encrypts the actual data (e.g., AES-256, ChaCha20).
  • Hashing Algorithm: Ensures data integrity (e.g., SHA256, SHA384).

Choosing the right cipher suite plays a crucial role. If weak cipher suites are used, the security level of the connection will significantly decrease, even if you have implemented TLS 1.3. The goal is to use only strong, modern cipher suites and completely eliminate outdated, less secure ones.

The Importance of Forward Secrecy (PFS)

Forward Secrecy (Perfect Forward Secrecy – PFS) is a critical security feature. It ensures that if a server’s long-term private key is compromised in the future, data from previous communication sessions remains secure and cannot be decrypted.

This mechanism works by using unique session keys that exist only for a short time and are generated independently for each session. TLS 1.3 by default requires and only supports key exchange mechanisms that provide Forward Secrecy, making the protocol significantly more secure.

Detailed Practice: Configuring Nginx to Achieve A+ on SSL Labs

Before diving into the configuration, I’d like to share a personal experience. When setting up servers, I always make sure to create strong passwords for services and administrative accounts. I often use the password generator at toolcraft.app/en/tools/security/password-generator. This tool generates passwords entirely in the browser, eliminating worries about password exposure over the network. This is an excellent habit that helps enhance overall system security, even when you have tightly configured TLS 1.3.

Now, let’s start configuring Nginx. I assume you already have Nginx running and an SSL/TLS certificate ready (e.g., from Let’s Encrypt). If not, you can refer to Certbot guides to obtain a certificate.

Step 1: Check Nginx Version and TLS 1.3 Support

First, verify that your Nginx has been compiled with TLS 1.3 support. Typically, Nginx versions 1.13.0 and newer already support TLS 1.3 if compiled with OpenSSL 1.1.1 or later. You can check using the command:


nginx -V

Look in the output for the line mentioning OpenSSL and its version. If you see OpenSSL 1.1.1 or higher, your Nginx is ready for TLS 1.3.

Step 2: Backup Current Configuration

Before making any changes, always back up your configuration files. This step helps you easily restore the system if errors occur.


sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo cp /etc/nginx/sites-available/your_domain.conf /etc/nginx/sites-available/your_domain.conf.bak

Replace your_domain.conf with the name of your website’s configuration file.

Step 3: Enable TLS 1.3 and Optimize Protocols

Open your Nginx configuration file (usually /etc/nginx/sites-available/your_domain.conf or /etc/nginx/nginx.conf, inside the http or server block). Find the ssl_protocols line and configure it as follows:


ssl_protocols TLSv1.2 TLSv1.3; # Prioritize TLS 1.3, still allow TLS 1.2 for compatibility

We recommend keeping only TLS 1.2 and TLS 1.3. Older versions like TLS 1.0 and TLS 1.1 are outdated and contain serious security vulnerabilities, so they should not be used.

Step 4: Optimize Cipher Suites

This is a critical part to achieve an A+ rating. We will remove weak cipher suites and retain only strong, modern ones. Always prioritize algorithms that provide Forward Secrecy (like ECDHE) and encryption algorithms such as AES-GCM or ChaCha20-Poly1305.

Add or modify the ssl_ciphers line:


ssl_ciphers 'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
  • Ciphers starting with TLS_ are for TLS 1.3.
  • Other ciphers (ECDHE-RSA-AES…) are for TLS 1.2.
  • ssl_prefer_server_ciphers on; ensures Nginx will prioritize the cipher suites it configures, rather than accepting any cipher proposed by the client.

Step 5: Configure Security Headers (HSTS, OCSP Stapling)

To achieve an A+, we need to add important security headers:

  • HTTP Strict Transport Security (HSTS): Forces browsers to always connect via HTTPS after the first visit, even if the user types HTTP.
  • OCSP Stapling: Helps speed up SSL certificate validation by having the server fetch and send certificate status information to the client itself, rather than the client having to query the OCSP responder.

# HSTS (applies to all subdomains for 2 years, only enable after the website is running HTTPS stably)
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;

# Path to the CA's root certificate, needed for OCSP Stapling
# Replace with the path appropriate for your system (e.g., from Let's Encrypt)
ssl_trusted_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;

Note on HSTS: Only enable preload when you are absolutely certain your website will always run HTTPS. Otherwise, users might not be able to access the website if you switch back to HTTP.

Step 6: Generate Strong Diffie-Hellman Key (DH Parameters)

To enhance security for TLS 1.2 (in case of fallback), we should use a stronger Diffie-Hellman key, such as 4096 bits, instead of the default key. Although TLS 1.3 uses other algorithms and thus relies less on this file, it’s still a good step for backward compatibility and overall security.


sudo openssl dhparam -out /etc/nginx/dhparam.pem 4096

This process may take a few minutes. After it’s done, add the following line to your Nginx configuration:


ssl_dhparam /etc/nginx/dhparam.pem;

Step 7: Optimize SSL Cache

Using SSL session cache helps speed up the re-establishment of TLS connections, improving performance for frequent users. This is a small but effective optimization.


ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d; # Keep TLS session for 1 day
ssl_session_tickets off; # Disable SSL session tickets to enhance security (optional)

Step 8: Test Configuration and Restart Nginx

After making all changes, it’s crucial to test the Nginx configuration to ensure there are no syntax errors before restarting the service.


sudo nginx -t

If you see the test is successful message, you can safely restart Nginx:


sudo systemctl restart nginx

If there are errors, recheck the steps and syntax in your configuration file. Typically, errors will be clearly indicated.

Step 9: Test with SSL Labs

The final step is to test your configuration using the SSL Labs SSL Test tool. Visit the website, enter your domain name, and click Submit. The testing process may take a few minutes.

If you have followed the steps correctly, congratulations! You will see an excellent A+ result! This proves that your website has been configured with the current leading TLS security level.

Conclusion

Configuring TLS 1.3 and optimizing cipher suites for Nginx not only makes your website more secure against threats but also significantly improves performance due to a faster handshake process. Achieving an A+ rating on SSL Labs is clear proof that you have implemented the best security practices. Always monitor and update your configuration to maintain the highest level of security, as the security landscape is constantly evolving.

Hopefully, this detailed guide will help junior developers easily master web server security for their Nginx instances. If you have any questions, don’t hesitate to leave a comment!

Share: