Chapter 3 · Lesson 20 of 22
Domains and HTTPS

HSTS explained: the Strict-Transport-Security header

HSTS is the Strict-Transport-Security response header that tells a browser to only connect to your site over HTTPS for a set number of seconds, even if a link or user types http.

By the end you will

Add a Strict-Transport-Security header in nginx, roll it out with a short max-age and verify it with curl.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • A site that already works over HTTPS
  • Access to your web server config

HSTS (HTTP Strict Transport Security) is a response header that tells browsers: for the next N seconds, never talk to this host over plain HTTP, only HTTPS. Without it, a visitor who types example.com or follows an old http link makes one insecure request that your server then redirects. That first request can be intercepted. HSTS closes that gap after the first secure visit. It is defined in RFC 6797.

How it works#

The header looks like this:

output
Strict-Transport-Security: max-age=31536000; includeSubDomains

When a browser receives it over a valid HTTPS connection, it stores the policy for the host. For the stored time, any attempt to load http://example.com is rewritten by the browser to https://example.com before any request leaves the machine. If the certificate is invalid, the browser also refuses to let the user click through a warning. That second point is protection against downgrade and interception attacks, and it is also why a certificate mistake becomes harder to work around.

Browsers ignore the header on plain HTTP responses, because an attacker on the path could inject it. It must come from a working HTTPS site. You still need a redirect for the first visit: see Redirect HTTP to HTTPS explained. HSTS builds on HTTPS and does nothing until HTTPS already works, so check HTTPS and Let’s Encrypt first.

The directives#

  • max-age=<seconds>: required. How long the browser remembers the policy. It is refreshed each time the header is seen.
  • includeSubDomains: optional. The policy also applies to every subdomain. Use it only when all current and future subdomains support HTTPS, including internal or forgotten ones like a test name.
  • preload: optional. A signal that you want the domain on the browser preload list. It does nothing by itself, see below.

Roll it out in stages#

Because a browser remembers the policy, the safe approach is gradual. Each stage should be observed before moving on.

  1. Confirm that every page and asset on the host works over HTTPS, and that the certificate renews automatically. See Renew Let’s Encrypt certificates.
  2. Start with a short value, such as five minutes (300 seconds), without includeSubDomains.
  3. After a day or so with no problems, raise it to something like a day, then a week.
  4. Later, consider a long value such as a year. Only add includeSubDomains once you have inventoried every subdomain.

If something breaks at stage 2, visitors recover within minutes rather than months. This is the main reason to avoid starting at a year.

nginx example#

Put the header in the HTTPS server block, not the port 80 block:

nginx
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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

The always flag makes nginx send the header on error responses too, not just 200s. Test and reload:

bash
sudo nginx -t && sudo systemctl reload nginx

Note that nginx does not inherit add_header lines into a block that defines its own add_header, so repeat the line in any location that sets other headers. See Nginx reverse proxy with certbot for the surrounding config. Caddy users can set it with a header directive, and with automatic HTTPS the redirect already exists.

Preload and why it is hard to undo#

The preload list ships inside browsers. A domain on it is treated as HSTS from the very first visit, with no prior secure request needed. The requirements are strict: a valid certificate, redirect from HTTP to HTTPS, a header with a long max-age, includeSubDomains and the preload token on the apex. The details are published by the list maintainers, so read the current requirements before you submit.

Getting off the list is a different story. Removal requires a request and then waits for browser releases to carry the change, and users on older versions keep the old behavior for a long time. Meanwhile, every subdomain you ever create must work over HTTPS. Because of this, treat preload as a one way door: do it only for a domain you are certain will serve HTTPS everywhere indefinitely, and never as a first step.

Check it worked#

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

You should see the header with your max-age. Also confirm that the plain HTTP address still redirects:

bash
curl -sI http://example.com | head -n 3

Expect a 301 or 308 status with a Location header pointing to the https address. In a browser you can check the stored policy in Chrome at chrome://net-internals/#hsts, where you can also query and delete an entry for a host while testing.

Edge cases and gotchas#

  • Self signed or expired certificate: with HSTS active, browsers will not offer the usual “continue anyway” option, so users are locked out until you fix the certificate.
  • includeSubDomains surprises: an old internal subdomain that only speaks HTTP becomes unreachable in browsers that saw the policy.
  • Bare IP addresses: HSTS applies to host names, not IP addresses. Do not expect it to protect a bare IP.
  • Header only on HTTPS: sending it from the port 80 block does nothing and is ignored.
  • Duplicate headers: a proxy and an application both adding the header can produce two values. Set it in one place.
  • Removal is slow: browsers that do not revisit never see max-age=0, and keep the old policy until it expires.

Troubleshooting#

SymptomLikely causeFix
Header missing in curl outputadd_header is in the wrong block or lacks alwaysAdd it to the 443 server block and reload nginx
Header seen on HTTP but browser ignores itBrowsers ignore HSTS on HTTPServe it over HTTPS
Subdomain now fails in browsersincludeSubDomains applies HTTPS-only to itGive it a certificate, or lower the policy and wait for expiry
Cannot click through a certificate warningHSTS forbids bypassFix or renew the certificate
Needed to undo HSTS quicklyLong max-age is cached in browsersServe max-age=0 over HTTPS and wait for revisits
Two Strict-Transport-Security lines in outputBoth proxy and app add itRemove one of them

Quick reference#

output
Strict-Transport-Security: max-age=300
Strict-Transport-Security: max-age=31536000; includeSubDomains
Strict-Transport-Security: max-age=0        (ask browsers to forget)
bash
curl -sI https://example.com | grep -i strict-transport-security

Next#

Continue with Mixed content explained and how to fix it.

Check what you learned4 questions
1What does max-age in the Strict-Transport-Security header mean?
2Why start with a short max-age?
3Where must the HSTS header be sent for browsers to honour it?
4Which command shows the HSTS header of a site?

Frequently asked questions

What is the HSTS header?

Strict-Transport-Security is a response header, for example Strict-Transport-Security: max-age=300. After a browser sees it over HTTPS, it refuses to use plain HTTP for that host until max-age seconds pass.

What does HSTS preload mean?

Preload adds your domain to a list built into browsers, so even the very first visit uses HTTPS. It requires specific header values and is slow and difficult to reverse, so use it only when every subdomain will stay HTTPS.

How do I remove HSTS?

Serve the header with max-age=0 over HTTPS so browsers drop the policy when they next visit. Browsers that never come back keep the old policy until it expires. Preloaded domains must be removed through the preload list process.

Does HSTS replace the HTTP to HTTPS redirect?

No. You still need the redirect for first visits and for clients that do not support HSTS. HSTS removes the insecure request on later visits.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios