HTTP, TLS & Web Performance

HSTS: Telling Browsers to Insist on HTTPS

Learn how the Strict-Transport-Security header works, roll it out safely in stages, understand preloading and recover from common mistakes.

Key takeaways

  • HSTS makes a browser convert HTTP requests to HTTPS before anything leaves the device.
  • The policy lasts for the number of seconds in max-age and is refreshed on each HTTPS visit that sends the header.
  • includeSubDomains extends the rule to every subdomain, including ones you may have forgotten about.
  • Preloading builds the rule into browsers, covers the very first visit and is slow to undo.
  • Start with a short duration, test everything, then increase it in steps.

HTTP Strict Transport Security (HSTS) is a response header that tells a browser: for this site, use only HTTPS from now on. After seeing it once, the browser upgrades every later request to HTTPS by itself and refuses to let the user click past certificate errors, until the policy expires.

It is a small line of configuration with a long memory. Rolled out carefully, it closes a real gap. Rolled out carelessly, it can make parts of your domain unreachable for visitors for months.

The gap HSTS closes

Many people type a bare domain, or follow an old http:// link. The server answers with a redirect to HTTPS, which works, but that first request and the redirect travel unprotected. Someone able to tamper with the network could intercept it and keep the victim on plain HTTP, or send them elsewhere. This is often called SSL stripping.

With HSTS in effect, the browser never sends the insecure request. It rewrites the address internally and connects over TLS from the start.

First visit and later visits with HSTS
Browser Website First visit over plain HTTP 1 301 redirect to HTTPS 2 HTTPS request 3 Page plus Strict-Transport-Security 4 Browser stores the policy Later: goes straight to HTTPS 5 No insecure request is sent Browser Website First visit over plain HTTP 1 301 redirect to HTTPS 2 HTTPS request 3 Page plus Strict-Transport- Security 4 Browser stores the policy Later: goes straight to HTTPS 5 No insecure request is sent

The header and its directives

HSTS is defined in RFC 6797. A full policy looks like this:

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
  • max-age is how long, in seconds, the browser remembers the rule. 31536000 is 365 days.
  • includeSubDomains applies the rule to all names under the one that sent it.
  • preload is a signal that you want inclusion in browser preload lists. It isn't part of RFC 6797 and does nothing by itself.

Two behaviors matter. Browsers ignore the header when it arrives over plain HTTP, since an attacker could forge it there. And they ignore it for connections made to a bare IP address.

What changes for visitors

While the policy is active:

  1. Typed, bookmarked and linked http:// addresses for the host are upgraded automatically.
  2. If the certificate is invalid, the browser shows an error with no option to proceed.

That second rule is deliberate. It stops people from being talked into accepting an impostor. It also means an expired certificate takes your site fully offline for returning visitors until you fix it, so automate renewal first. ACME automation helps, and certificate error codes explains what visitors would see.

Before you enable it

Confirm the following:

  • Every page on the host works over HTTPS, with no mixed content problems.
  • HTTP requests redirect to HTTPS on the same hostname.
  • Certificate renewal is automatic and monitored.
  • If you plan to include subdomains, you have an inventory of all of them: internal tools, old marketing sites, device interfaces and services run by third parties.

A DNS zone export is a good starting point for the inventory. Certificate transparency logs can reveal names you forgot.

Adding the header

In nginx, inside the HTTPS server block:

add_header Strict-Transport-Security "max-age=300" always;

In Apache, with mod_headers enabled, inside the HTTPS virtual host:

Header always set Strict-Transport-Security "max-age=300"

CDNs, proxies and hosting panels often provide a switch for the same thing. Set it in one place to avoid sending duplicate or conflicting headers. Note that in nginx, an add_header in a nested block replaces inherited ones, so check responses from several paths:

curl -sI https://example.com/ | grep -i strict-transport-security

A staged rollout

Raising HSTS commitment gradually
1 Five minutes max-age=300; test pages and tools 2 One week max-age=604800; watch for reports 3 One month Confirm renewals and edge cases 4 One year max-age=31536000 once stable 5 Subdomains and preload Only after a full inventory Five minutes max-age=300; test pages and tools 1 One week max-age=604800; watch for reports 2 One month Confirm renewals and edge cases 3 One year max-age=31536000 once stable 4 Subdomains and preload Only after a full inventory 5

At each stage, leave enough time for problems to surface. If something breaks, you only have to wait out the current duration. You can add includeSubDomains early in the staged process, while max-age is still short, which is the cheapest time to find a forgotten HTTP-only subdomain.

Place the header on the apex domain, for example example.com, if you want it to cover all subdomains. A policy sent by www.example.com covers only www and anything below it. If users mostly visit www, redirect the apex over HTTPS first so browsers see the apex policy.

Preloading

HSTS has a weak spot: the first visit, before the browser has seen any header. The preload list solves it by shipping a list of HTTPS-only domains inside the browser. The list is maintained through the submission site hstspreload.org and used by major browsers.

Its published requirements have included a valid certificate, an HTTP to HTTPS redirect, and a header on the base domain with a max-age of at least one year, includeSubDomains and preload. Check the current terms there.

Some whole top-level domains, such as .dev and .app, are preloaded, so any site under them requires HTTPS from the start.

Preloading is a long commitment. Removal is possible, but it takes time to reach users, who must receive browser updates. If any subdomain might ever need plain HTTP, don't preload.

Header alone versus preloading
Header only Protects after the first HTTPS visit Expires after max-age Can be withdrawn with max-age=0 Preloaded Protects even the first visit Built into browser releases Slow and awkward to reverse VS Header only Protects after the first HTTPS visit Expires after max-age Can be withdrawn with max-age=0 Preloaded Protects even the first visit Built into browser releases Slow and awkward to reverse

Subdomains run by other people

includeSubDomains reaches names you may not operate yourself. A help desk, status page, store or email tracking host might be a subdomain of yours that points at an outside provider. If that provider can't serve a valid certificate for your name, the service stops working for anyone holding your policy.

Ask each provider whether it supports HTTPS on custom domains, and test before you include subdomains. Link tracking hosts used by mail services deserve special attention, since some serve plain HTTP by default.

Internal names are the other trap. If your company uses intranet.example.com or a device interface under the public domain, those need certificates too. Because the browser, not your network, enforces the rule, the policy follows staff back into the office. Options include issuing certificates through a DNS challenge, running a private CA on managed devices or moving internal names to a separate domain.

Old subdomains that still point at services you've cancelled should be removed from DNS altogether. They are a security risk in their own right, and cleaning them up shrinks the inventory you have to protect.

Turning it off and testing

To cancel a policy, serve this over a working HTTPS connection:

Strict-Transport-Security: max-age=0

Each browser forgets the rule only when it next visits and receives that header. If your certificate is broken, you have to repair it before the cancellation can be delivered.

While testing on your own machine, Chromium-based browsers offer a page at chrome://net-internals/#hsts (the prefix varies by browser) where you can query or delete a stored policy. Preloaded entries can't be removed there. Firefox clears stored policy when you tell it to forget a site, though the exact menu wording varies.

APIs and programs

HSTS was designed with browsers in mind. If you run an API, a redirect from HTTP is risky in its own way: a misconfigured program may send its access key in the clear before it ever sees your redirect, and then follow along so smoothly that nobody notices.

Many API operators therefore refuse plain HTTP requests with an error instead of redirecting them, so the mistake shows up during development. Some also revoke a key that arrives unencrypted. Sending the HSTS header on API responses does no harm and helps browser-based callers, but don't count on other clients to remember it. Write https:// into your documentation and client libraries, and test that nothing falls back.

Troubleshooting

  • A subdomain stopped loading. It lacks working HTTPS and is now covered by includeSubDomains. Add a certificate; that is faster than waiting out the policy.
  • Local development redirects to HTTPS. A cached policy covers the name you're using. Delete it, or use a different test hostname.
  • The header doesn't appear. It may be set only on the HTTP block, stripped by a proxy or omitted on error responses.
  • Redirect chains. Redirect http://example.com to https://example.com first, then to www if you use it, so each host sets its own policy.

What HSTS doesn't do

It doesn't fix weak TLS settings, protect against phishing domains or secure your application. It works only in clients that implement it; many scripts and libraries ignore it unless configured. Cookies still need the Secure attribute, as covered in how HTTP cookies work. Think of HSTS as the rule that makes your HTTPS setup mandatory.

Common questions

Does HSTS replace the HTTP redirect?

No. New visitors and clients without the policy still arrive over HTTP and need the redirect.

Can HSTS be used to track people?

Researchers have described ways of abusing stored policies across many subdomains as an identifier, and browsers have added restrictions. For an ordinary site with a straightforward policy, this isn't something you need to act on.

Should a small site use it?

If your HTTPS setup is solid and renewal is automatic, yes, with at least a moderate duration. Preloading can wait until you're sure.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in HTTP, TLS & Web Performance →

Latest news on this topic