You have a certificate and the site opens over https, but that doesn't mean HTTPS is set up well. Old configurations still have TLS 1.0 and 1.1 enabled, the site still answers on plain http without a redirect, and the security headers vanish on half the pages. Here is what a proper nginx configuration looks like and how to check it.
Redirecting http to https
A separate server block on port 80 does nothing but redirect, straight to the main address, so the visitor makes one hop instead of two:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
If the certificate is renewed by HTTP-01 validation in webroot mode, add location /.well-known/acme-challenge/ { root /var/www/letsencrypt; } before the redirect. Let's Encrypt does follow redirects, but a separate folder is more reliable.
The main HTTPS block
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=63072000" always;
server_tokens off;
}
ssl_protocols TLSv1.2 TLSv1.3: TLS 1.0 and 1.1 are obsolete and unsupported by modern browsers, and SSL 3 has been unsafe for a long time.http2 onis a directive of nginx 1.25.1 and later; older versions uselisten 443 ssl http2;.- Ciphers. For TLS 1.3 nginx picks them itself. TLS 1.2 needs an
ssl_cipherslist: use Mozilla's recommendations, not lists from old articles with RC4 and 3DES. ssl_session_tickets off: without regular rotation of the ticket key, session tickets weaken forward secrecy.server_tokens offstops nginx from announcing its version in headers and error pages.
A ready block following Mozilla's recommendations, with the full TLS 1.2 cipher list, HSTS and the redirect, for nginx, Apache and HAProxy, is built by TLS configuration.
OCSP stapling
Old guides always turn on ssl_stapling on. Let's Encrypt dropped OCSP in 2025, and its certificates no longer carry an OCSP server address, so stapling isn't needed for them: nginx would only write warnings to the log. For certificates from other authorities, stapling is still useful.
HSTS: careful with includeSubDomains and preload
The Strict-Transport-Security header tells the browser to open the domain only over HTTPS, even if the user typed http. It protects against interception of the first request, but it can't be undone: the browser remembers the header for the whole max-age.
- Start with a short period, such as
max-age=300, check the site, and only then set one or two years. - Add
includeSubDomainsonly if all subdomains work over HTTPS, including internal ones, admin panels and old services. preloadputs the domain into lists built into browsers. Removal takes months, so don't add it unless you are sure.
The add_header trap
Headers from add_header are inherited by a location only if that location has no add_header of its own. Add a Cache-Control header to location ~ \.(css|js)$, and HSTS, CSP and the other headers disappear from those files. The fix is to move the security headers into a separate file and include it in each such location.
The second point is the word always: without it nginx doesn't add the header to error responses (404, 500), and those need protection too.
HSTS, Content-Security-Policy, X-Frame-Options, Referrer-Policy and Permissions-Policy are built by HTTP security headers, which can also check a finished CSP for mistakes.
Requests for unknown domains
Scanners often contact the server by its IP address or someone else's name. Without a dedicated block, nginx answers with the first site in the configuration and shows its certificate, which tells the internet which sites live on the server. A default block simply drops such connections (nginx 1.19.4 and later):
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_reject_handshake on;
}
Checking
sudo nginx -t && sudo systemctl reload nginx curl -sI http://example.com | head -3 # 301 to https curl -sI https://example.com | grep -i strict # HSTS is present openssl s_client -connect example.com:443 -servername example.com -tls1_1 </dev/null # the last command must fail: TLS 1.1 is disabled
If you need to set up nginx from scratch (a proxy to an application, static files, PHP-FPM, with HTTPS, gzip, caching and request limits), use the nginx config generator. How to get and renew a certificate is covered in the article on Let's Encrypt, and for Apache and LiteSpeed there is the .htaccess generator.