How to Add HSTS (Strict-Transport-Security) Safely
What HSTS protects against, the exact header to send, a safe rollout plan, and how to add it on Next.js, Vercel, Cloudflare and nginx.
By Jay Dabhi, CISM · Cybersecurity Analyst
Updated
To add HSTS, send this response header on every HTTPS response: Strict-Transport-Security: max-age=31536000; includeSubDomains. It tells browsers to use HTTPS for your domain for a year, even if someone types http:// or clicks an old link. Roll it out with a short max-age first, confirm every subdomain works over HTTPS, then raise it to a year.
What HSTS protects against
Without HSTS, a visitor's first request can go over plain HTTP before your server redirects them to HTTPS. On hostile Wi-Fi, an attacker can intercept that request and keep the visitor on an HTTP copy of your site (an SSL-stripping attack). With HSTS, the browser upgrades to HTTPS itself, before any request leaves the device.
The header
Strict-Transport-Security: max-age=31536000; includeSubDomains
max-age: how long browsers remember the rule, in seconds. 31536000 is one year.includeSubDomains: applies the rule to every subdomain too.preload: only add this if you're submitting to the browser preload list (see the FAQ).
Safe rollout
- Check every subdomain serves HTTPS. Our subdomain finder lists the hostnames visible in certificate logs. Make sure none of them still needs plain HTTP.
- Start short. Send
max-age=300(five minutes) for a day. - Increase in steps: one week (
604800), one month (2592000), then one year (31536000).
If something breaks, lower the value. Browsers that already cached the long value keep it until it expires, which is why you start short.
Adding it on your stack
Next.js (next.config.ts):
async headers() {
return [{
source: "/:path*",
headers: [{ key: "Strict-Transport-Security", value: "max-age=31536000; includeSubDomains" }],
}];
}
Vercel: add the same header in vercel.json or vercel.ts under headers.
Cloudflare: SSL/TLS → Edge Certificates → HTTP Strict Transport Security (HSTS).
nginx:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Check your work
Run your site through our security headers checker. It confirms the header is present and warns if max-age is shorter than 180 days.
Frequently asked questions
Can HSTS break my site?
Yes, if any subdomain still needs plain HTTP. With includeSubDomains, browsers refuse HTTP for every subdomain for the whole max-age. Start with a short max-age and confirm every subdomain works over HTTPS before increasing it.
Should I submit my domain to the HSTS preload list?
Only when you're sure every subdomain will serve HTTPS permanently. Preloading builds HTTPS-only into browsers themselves, and removing a domain takes months. Most startups get nearly all the benefit without preloading.
Does HSTS replace an HTTP to HTTPS redirect?
No, keep both. The redirect handles a visitor's very first request; HSTS makes the browser skip HTTP on every visit after that.