HTTP Strict Transport Security (HSTS) is an HTTP response header that instructs the browser to always connect via HTTPS, even if the user types 'http://' or accesses from a link without a protocol. Once the browser receives this header, it remembers the policy for the duration specified in max-age and rejects any unencrypted connection to the domain without attempting it.
The Problem HSTS Solves: SSL Stripping
Even if your site redirects from HTTP to HTTPS, without HSTS an attacker on the same network (public Wi-Fi, compromised corporate network) can intercept the initial HTTP request before the server can redirect the user. The attacker acts as a man-in-the-middle, serving the site in plain text while communicating with the server over HTTPS. This attack is called SSL Stripping and is completely invisible to the user. With HSTS active, the browser directly rejects any HTTP connection attempt, eliminating this attack vector.
Implementation in Nginx
Implementation in Apache
Implementation in Cloudflare
If using Cloudflare as a proxy (orange cloud), go to the dashboard: SSL/TLS → Edge Certificates → HTTP Strict Transport Security (HSTS). Enable HSTS, set max-age to 12 months, and check 'Include subdomains' and 'Preload' only if all your subdomains serve HTTPS correctly. Cloudflare automatically injects the header into all responses.
HSTS Parameters Explained
- max-age=31536000: the browser remembers the policy for 1 year (31,536,000 seconds). The minimum recommended for preloading is exactly 1 year.
- includeSubDomains: applies HSTS to all subdomains. Only enable this if subdomains like mail., api. or blog. also serve HTTPS.
- preload: signals that the domain wants to be included in the HSTS Preload List, which is built directly into Chrome, Firefox, Safari and Edge.
HSTS Preloading: Protection from the Very First Visit
The HSTS Preload List is hardcoded into browsers. Included domains have HSTS active by default even before the first visit, eliminating the only instant when an SSL Stripping attack could work. To request inclusion, visit hstspreload.org. Important: once included in the list, reverting can take months — only request it when all subdomains work correctly over HTTPS.
Most Common Mistakes
- Enabling includeSubDomains without HTTPS on all subdomains: those subdomains will become inaccessible since the browser will refuse the HTTP connection.
- max-age below 10,886,400 seconds (126 days): Google Search Console warns about very short values.
- Sending the HSTS header in the HTTP VirtualHost (port 80): browsers ignore HSTS on insecure connections.
- Forgetting the 'always' parameter in Nginx: the header won't be sent in error responses (4xx, 5xx).
DomainScored automatically checks the HSTS header in the HTTP Headers module: whether it's active, the max-age value, and whether it includes subdomains and preload. The analysis includes exact fix instructions for your server.