SiteHealthRadar

Guides

How do you add HSTS on nginx?

By Md Jewele Islam. 18 November 2026.

Add one line to your HTTPS server block, with a short lifetime to begin with:

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

Run sudo nginx -t, reload, and read the header back. Raise the number once you're sure. This guide covers that, the two ways the line silently stops working, and how to back out. For an overview of all the security headers, see the headers guide.

Why start so low?

HSTS tells a browser to use only HTTPS for your site, until the time runs out. If your certificate then fails, the browser won't let visitors click past the error (RFC 6797, section 12.1). In our Chromium test, the warning page for an HSTS site had no "Proceed" link at all, and said: "You cannot visit hsts.test right now because the website uses HSTS." The same broken certificate on a site without HSTS still offered "Proceed to plain.test (unsafe)". So a mistake lasts as long as the number you chose. 300 is five minutes.

Before you start, make sure HTTPS works everywhere on the site, and that renewal is working.

How do you read it back?

curl -sI https://example.com/ | grep -i strict
curl -sI https://example.com/no-such-page | grep -i strict

Check both a normal page and a 404. Each should print the header.

Why does the header go missing on some pages?

No always. Without it, nginx adds the header only when the response code is 200, 201, 204, 206, 301, 302, 303, 304, 307 or 308 (nginx docs). In our test, the header was on the 200 and gone from the 404.

A block with its own add_header. Directives are inherited only if the current level has none. In our tests:

  • A location with its own add_header Cache-Control lost HSTS on that path. Adding add_header_inherit merge; brought it back, on nginx 1.30.6 and on the current mainline, 1.31.8.
  • HSTS set in http reached a server with no add_header, and was gone from a server that set its own. Same on 1.18.0, 1.30.6 and 1.31.8.

The simplest fix on 1.29.3 or later is one line next to your HSTS line, at the top of the config (the http level):

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

On 1.31.8, that kept HSTS on every path we tried: a server with its own header, a location with its own Cache-Control, and a 404 from that location. Put in one location only, merge brings in the headers of the block just above it, not the ones higher up. And add_header_inherit off; removes every inherited header from that block, HSTS included.

add_header_inherit isn't available on older versions: nginx 1.18.0 refused it with unknown directive. nginx's docs say 1.29.3 and later. On anything older, repeat the HSTS line in that block.

Does it matter on the HTTP redirect?

nginx will send it there if you put the line in that block. Our test showed the header on a plain-HTTP 301. It does nothing: browsers must ignore HSTS received over HTTP (section 8.1). In our Chromium test, a site that sent the header only over HTTP stayed on HTTP on the next visit. The browser only learns it from an HTTPS response it trusts, so a self-signed test server won't teach a browser anything.

Once the browser has learned it, typing http:// never reaches port 80: in our test, nginx logged no request on port 80 and the browser went straight to HTTPS, also after the browser was closed and opened again.

How far do you raise max-age?

Sources differ, because they're talking about different moments. hstspreload.org ramps up from 5 minutes. Mozilla's guide gives at least six months as the end state. A sensible path: 300, then a day, a week, a month, a year. Wait at each stage, and watch for problems.

Don't stop at a short number. A short max-age protects a visitor only for a short time after their last visit, which is why many guides and linters treat it as a mistake. One linter, Gixy, flags anything under six months. The low numbers are for the rollout only. The goal is a year or more.

In our test, max-age=5 sent visitors to HTTPS for the first few seconds, and after 7 seconds http:// loaded over plain HTTP again. So the number really is a countdown from the last visit.

What about includeSubDomains and preload?

Leave both out until you've checked every subdomain, including old ones nobody remembers. includeSubDomains applies the rule to all of them. Preload builds the rule into browsers, and hstspreload.org warns that removal is slow. hstspreload.org's own stages include includeSubDomains from the first five minutes, because that site is for domains heading to the preload list. If you're not preloading, add it last.

What if you're behind a CDN?

Set it in one place. If your CDN adds it and your origin does too, check with curl what actually reaches visitors.

When you change the certificate or the header, check from curl or a newly opened browser, not a tab you already had open. In our test, a browser that was already connected loaded the page with no error after we swapped in a bad certificate; a freshly started one saw the error.

How do you undo it?

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

RFC 6797, section 6.1.1 says zero tells the browser to stop treating your site as HSTS. In our test, one HTTPS visit that got max-age=0 was enough: the next http:// visit stayed on HTTP. But a browser has to reach your site over working HTTPS to hear it. Anyone who doesn't visit keeps the old rule until it runs out. So keep the certificate valid until then.

What was tested, and what wasn't

On 3 November 2026, in throwaway Docker containers with self-signed certificates and curl -k: nginx 1.30.6 for always, location inheritance, add_header_inherit merge and the HTTP 301; nginx 1.18.0, 1.30.6 and 1.31.8 for http inheritance. On 5 November 2026, nginx 1.31.8 (the official nginx:1.31.8-alpine image) for add_header_inherit merge and off at http, server and location level.

On 3 November 2026, in a throwaway container: nginx 1.26.3 and headless Chromium 131 (Alpine package), with a test certificate authority trusted only inside that container. We checked the switch to HTTPS, that it survives a browser restart, the missing "Proceed" link, HSTS sent over HTTP being ignored, max-age=0, and a 5-second max-age running out.

Not tested: other browsers (Firefox, Safari), a normal desktop Chromium window, preload, includeSubDomains, a CDN's own HSTS setting (such as Cloudflare's), and the long stages over days.

Check your site

The free security headers checker reads the headers from your homepage. It looks at that one page, so it won't catch a location that lost the header, and it reports HSTS as present or missing. Rather not touch the config? We quote per job.

Sources were read on 3 to 5 November 2026 and checked again on 6 November 2026.