Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Header

Strict-Transport-Security: max-age=300

A server sends this header on HTTPS responses. The browser stores the rule for the host name and, for the number of seconds in max-age, uses HTTPS for every request to it, even when a visitor types the plain address or follows an old http:// link. Browsers ignore the header when it arrives over plain HTTP, because an attacker could inject it there.

Why It Exists

A redirect from HTTP to HTTPS still leaves the first request unencrypted. On an untrusted network, an attacker can intercept that request and keep the visitor on a plain HTTP copy of the site. With HSTS, the browser never makes the plain request in the first place. It also turns certificate warnings for the site into hard errors, so visitors cannot be talked into accepting a forged certificate. Background on the two protocols is in HTTP vs HTTPS.

Before You Turn It On

  • HTTPS works everywhere: a valid certificate on every name the header covers, with automatic renewal. See Certbot with Nginx and Certbot renew.
  • HTTP redirects to HTTPS: set the redirect up first, as in how to redirect HTTP to HTTPS in Nginx.
  • Start short: once browsers have the rule, you cannot take it back from them before it expires. If HTTPS breaks during that time, visitors cannot reach the site at all. Begin with max-age=300 (five minutes).

Add It in Nginx

In the server block that listens on 443, not in the port 80 block:

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

always adds the header whatever the response code, so error pages and redirects carry it too. Note that a location block with any add_header of its own does not inherit the ones from the server block, so repeat the line there. Test the configuration, and reload only if the test passes:

sudo nginx -t && sudo systemctl reload nginx

Add It in Apache

Inside the <VirtualHost *:443> block:

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

The line needs mod_headers. On Debian and Ubuntu, enable it and test:

sudo a2enmod headers
sudo apache2ctl configtest && sudo systemctl reload apache2

On AlmaLinux and Rocky Linux the module is normally loaded already:

sudo httpd -t && sudo systemctl reload httpd

Check It with curl

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

The output should show the header with your max-age. Run the same against http://example.com: you should see the redirect and no HSTS header there.

Raise max-age Step by Step

When the site has run without HTTPS problems for a while, raise the value in stages, for example to one day (86400), then one week (604800), then one year (31536000). Each step commits browsers for longer, so watch certificate renewals and errors between steps.

Subdomains and Preload: Hard to Undo

Two directives extend the rule:

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
  • Subdomains: includeSubDomains applies the rule to every name under the domain, including internal tools, test sites, and devices that may have no valid certificate. Add it only after checking every subdomain, because browsers that saw it keep enforcing it until max-age runs out.
  • Preload: preload marks the domain for the preload list built into browsers, which enforces HTTPS even on a first visit. Submission requires a valid certificate, an HTTP to HTTPS redirect on the same host, HTTPS on every subdomain, and a header on the base domain with a max-age of at least 31536000 (one year) plus both directives. Removal is possible but takes months to reach visitors, since the change has to ship in browser updates. Treat preloading as close to permanent.

Turning It Off

To withdraw HSTS, keep serving HTTPS and send max-age=0. Each browser drops the rule the next time it visits over HTTPS. Browsers that do not return keep the old rule until it expires. More about the protocol is in what is SSL/TLS.

Was this answer helpful?
Loading...