# 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.

URL: https://levelrail.com/learn/domains-https/hsts-explained/
Updated: 2026-11-27

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:

```text
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](https://levelrail.com/learn/domains-https/redirect-http-to-https-explained/)). HSTS builds on [HTTPS](https://levelrail.com/learn/glossary/#https)) and does nothing until HTTPS already works, so check [HTTPS and Let's Encrypt](https://levelrail.com/learn/domains-https/https-and-lets-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](https://levelrail.com/learn/domains-https/renew-lets-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](https://levelrail.com/learn/domains-https/nginx-reverse-proxy-with-certbot/)) for the surrounding config. Caddy users can set it with a `header` directive, and with [automatic HTTPS](https://levelrail.com/learn/domains-https/automatic-https-with-caddy/)) 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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Header missing in curl output | add_header is in the wrong block or lacks `always` | Add it to the 443 server block and reload nginx |
| Header seen on HTTP but browser ignores it | Browsers ignore HSTS on HTTP | Serve it over HTTPS |
| Subdomain now fails in browsers | includeSubDomains applies HTTPS-only to it | Give it a certificate, or lower the policy and wait for expiry |
| Cannot click through a certificate warning | HSTS forbids bypass | Fix or renew the certificate |
| Needed to undo HSTS quickly | Long max-age is cached in browsers | Serve max-age=0 over HTTPS and wait for revisits |
| Two Strict-Transport-Security lines in output | Both proxy and app add it | Remove one of them |

## Quick reference

```text
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
```

## Related lessons

- [Redirect HTTP to HTTPS explained](https://levelrail.com/learn/domains-https/redirect-http-to-https-explained/)): the redirect HSTS builds on.
- [Mixed content explained](https://levelrail.com/learn/domains-https/mixed-content-explained/)): fix insecure assets before forcing HTTPS.
- [TLS versions and ciphers in plain language](https://levelrail.com/learn/domains-https/tls-versions-and-ciphers-explained/)): the other half of a secure server config.
- [Renew Let's Encrypt certificates](https://levelrail.com/learn/domains-https/renew-lets-encrypt-certificates/)): an expired certificate plus HSTS locks users out.
- [Nginx reverse proxy with certbot](https://levelrail.com/learn/domains-https/nginx-reverse-proxy-with-certbot/)): where the header belongs.

## Next

Continue with [Mixed content explained and how to fix it](https://levelrail.com/learn/domains-https/mixed-content-explained/)).

## 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

- [RFC 6797, HTTP Strict Transport Security (HSTS)](https://www.rfc-editor.org/rfc/rfc6797)
