All guides
Term

HSTS

A security policy mechanism that forces browsers to communicate with a server exclusively over encrypted HTTPS connections, blocking insecure HTTP access.

HSTS (HTTP Strict Transport Security) is a web security policy mechanism that forces browsers to communicate with a server exclusively over encrypted HTTPS connections, blocking insecure HTTP access. Delivered as an HTTP response header, it mitigates Man-in-the-Middle (MitM) attacks such as SSL stripping, where an attacker intercepts an initial plaintext request and prevents the client from upgrading to an encrypted session. It also stops users from clicking through browser warnings caused by invalid or expired TLS certificates.

How it works

When a browser connects to a site over HTTPS and receives the Strict-Transport-Security header, it caches the policy for the exact number of seconds specified by the max-age directive. During that window, if a user types http:// into the address bar or clicks an unencrypted link, the browser performs an internal 307 Internal Redirect to https:// locally before any packet leaves the network interface.

Because a standard HSTS header only takes effect after a browser's first successful HTTPS connection, the very first visit remains vulnerable to interception—a limitation known as Trust On First Use (TOFU). To close this gap, site administrators can submit their root domain to the HSTS Preload List. Major browsers like Chrome, Firefox, and Safari compile this list directly into their source code, enforcing HTTPS connections even on a user's initial visit.

Browsers strictly ignore the HSTS header if it is transmitted over an unencrypted HTTP connection or over an HTTPS connection with certificate errors. This prevents attackers on hostile networks from injecting malicious or arbitrarily long HSTS policies.

Example

A production-ready HSTS header instructs the browser to enforce HTTPS for the apex domain and all subdomains for a long duration while signaling eligibility for browser preloading:

Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

In an Nginx server block listening on port 443, you apply this directive globally across all responses using the always parameter so that error pages also carry the header:

server {
    listen 443 ssl;
    server_name example.com https://www.example.com;

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
}

What matters when choosing a server or hosting

  • Automated TLS management: Because HSTS blocks users from bypassing expired certificates, your hosting platform or reverse proxy must support reliable, automated certificate issuance and renewal (such as ACME/Let's Encrypt) without downtime.
  • Custom response header control: Ensure your web server, CDN, or managed hosting provider allows you to inject custom HTTP headers and configure the always flag so non-200 status codes still enforce the policy.
  • Subdomain inventory visibility: Before enabling includeSubDomains, verify that your DNS and hosting setup does not host legacy staging servers, internal dashboards, or third-party white-label tools on subdomains that lack valid TLS certificates.

Common mistakes

  • Deploying a long max-age immediately: Setting a multi-month duration on day one locks visitors out if a misconfigured certificate breaks HTTPS. Start with a short value like max-age=300, verify all assets load cleanly over TLS, and scale up gradually.
  • Prematurely adding includeSubDomains: Applying this directive breaks access to any subdomain—such as mail.example.com or a legacy intranet—that still relies on HTTP or uses a self-signed certificate.
  • Submitting to preload without a rollback plan: Removing a domain from the browser preload list takes months to propagate through browser release cycles. Only add the preload directive once every subdomain permanently supports HTTPS.

FAQ

Does HSTS replace the need for a 301 HTTP-to-HTTPS redirect?

No. You still need a standard server-side 301 Moved Permanently redirect on port 80 to upgrade clients that have not yet cached the HSTS policy or do not support HSTS.

How do I disable HSTS if something goes wrong during testing?

Send the header over a valid HTTPS connection with max-age=0. As soon as a browser receives this response, it immediately expires the cached policy for that domain.

Why is my browser ignoring the HSTS header I configured?

Browsers silently discard the header if it arrives over plain HTTP, if the TLS certificate is self-signed or mismatched, or if your web server omits the header on redirect and error responses.

All glossary terms

Glossary

Related terms

Full glossary

New guides every week.

A digest of the best pieces on servers, VPN and self-hosted software. No ads, no spam.

1 email a weekOne-click unsubscribeRU · EN
Only slipjar.app articles, nothing else.