HTTP Strict Transport Security (HSTS) tells browsers to connect to your site only over HTTPS for a set period of time. Once a browser has seen the header, it rewrites every http:// link to https:// on its own, before any request leaves the computer. That closes the gap an HTTP to HTTPS redirect leaves open: the first plain-HTTP request, which an attacker on the same Wi-Fi or network path can intercept to keep the victim on an unencrypted connection (an “SSL stripping” attack). HSTS also stops visitors from clicking through certificate warnings on your domain.
This guide shows how to enable HSTS in Nginx safely: with a reusable snippet, a gradual rollout so a mistake can’t lock visitors out, fixes for the two Nginx behaviours that silently drop the header, and the optional step of joining the browser preload list.
Last tested: November 2026 with Nginx 1.30 (stable) and 1.31 (mainline). The snippet approach works on every version, including Nginx 1.24 on Ubuntu 24.04.
Quick Answer
Add this line to the HTTPS (port 443) server block of your site, then run sudo nginx -t && sudo systemctl reload nginx:
server {
listen 443 ssl;
server_name example.com;
# ... ssl_certificate, root, etc.
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
}
Before you do that on a live site, read the rollout steps below. A long max-age is cached in visitors’ browsers and can’t be recalled if a subdomain turns out not to support HTTPS.
The HSTS Header Explained
| Directive | What it does |
|---|---|
max-age=63072000 | How many seconds the browser remembers the policy. 63072000 is two years. Each visit restarts the countdown. max-age=0 removes the policy. |
includeSubDomains | Applies the policy to every subdomain too (www, api, mail, internal hosts). Leave it out until every subdomain works over HTTPS. |
preload | Signals that you want your domain hard-coded into browsers’ HSTS preload list. It does nothing by itself; you must also submit the domain. Only add it when you are ready for that (see Step 6). |
Browsers ignore HSTS headers received over plain HTTP, as required by the HSTS specification (RFC 6797), so sending the header from your port 80 server block has no effect. The header only counts when it arrives over a valid HTTPS connection.
Before You Start
- Your site works over HTTPS with a valid, trusted certificate.
- HTTP already redirects to HTTPS. Set that up first with our guide to redirect HTTP to HTTPS in Nginx.
- If you plan to use
includeSubDomains: every subdomain works over HTTPS, includingwww, staging, mail web interfaces and internal tools that only resolve on your network. A subdomain that only works on HTTP becomes unreachable for anyone who has your HSTS policy.
Step 1 of 6Create an HSTS Snippet
Keep the header in one small file, so every site and every location that needs it uses the same value, and you can change max-age in one place. Create the snippets directory (it already exists on Ubuntu and Debian) and the file:
sudo mkdir -p /etc/nginx/snippetssudo nano /etc/nginx/snippets/hsts.conf
Start with a five-minute max-age. If something breaks, browsers forget the policy almost immediately:
add_header Strict-Transport-Security "max-age=300; includeSubDomains" always;
The always parameter matters. Without it, Nginx adds the header only to successful responses (2xx and 3xx) and leaves it off error pages. In our test, a 404 page without always had no HSTS header; with always, it did.
Step 2 of 6Include It in Every HTTPS Server Block
Add the snippet to each server block that listens on port 443, including server blocks that only redirect (such as https://www → https://example.com):
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include snippets/hsts.conf;
root /var/www/example.com;
}
Don’t put it in your port 80 server blocks: browsers would ignore it there anyway.
Step 3 of 6Avoid the add_header Inheritance Trap
This is the most common reason HSTS “doesn’t work” in Nginx. A location (or if) block inherits add_header directives from the server block only if it has no add_header of its own. As soon as you add any header in a location, for example a cache header for static files, all inherited headers are dropped, including HSTS:
server {
# ...
include snippets/hsts.conf;
location /static/ {
# This add_header replaces ALL headers inherited from the server block,
# so /static/ responses lose HSTS.
add_header Cache-Control "public, max-age=86400";
}
}
In our test, /static/ responses from this config had the Cache-Control header but no HSTS header. There are two fixes:
Any Nginx version: include the snippet again in every location that has its own add_header:
location /static/ {
add_header Cache-Control "public, max-age=86400";
include snippets/hsts.conf;
}
Nginx 1.29.3 and newer (including the 1.30 stable branch): tell the location to merge its headers with the inherited ones. Distribution packages such as Ubuntu 26.04 (1.28), Debian 13 and RHEL 10 (1.26) and Ubuntu 24.04 (1.24) don’t support this directive yet and stop with unknown directive "add_header_inherit":
location /static/ {
add_header_inherit merge;
add_header Cache-Control "public, max-age=86400";
}
Find every location that might be affected with:
sudo grep -rn 'add_header' /etc/nginx/
Step 4 of 6Test and Reload
sudo nginx -tsudo systemctl reload nginx
Check the header on a normal page, an error page and any location with its own headers. With the final max-age, the output looks like this:
$ curl -sI https://example.com/ | grep -i strict
strict-transport-security: max-age=63072000; includeSubDomains
$ curl -sI https://example.com/does-not-exist | grep -iE "^HTTP|strict"
HTTP/2 404
strict-transport-security: max-age=63072000; includeSubDomains
Also check each subdomain you serve and the https://www redirect. To see what your own browser has stored, Chrome and Edge offer an HSTS query and delete tool at chrome://net-internals/#hsts, which is handy while testing.
Step 5 of 6Increase max-age Gradually
Raise max-age in stages. At each stage, watch for broken pages, mixed-content errors and unreachable subdomains, and wait the full max-age of the stage before moving on. This schedule follows the recommendation on hstspreload.org:
| Stage | Header value | Wait before next stage |
|---|---|---|
| 1. Test | max-age=300; includeSubDomains | 5 minutes, plus time to test |
| 2. One week | max-age=604800; includeSubDomains | 1 week |
| 3. One month | max-age=2592000; includeSubDomains | 1 month |
| 4. Final | max-age=63072000; includeSubDomains | Keep it |
For each stage, change the value in /etc/nginx/snippets/hsts.conf and reload Nginx. If any subdomain can’t move to HTTPS, leave out includeSubDomains. You still protect the main domain, but you can’t join the preload list.
Step 6 of 6Optional: Join the HSTS Preload List
HSTS only protects visitors after their first HTTPS visit. The preload list is built into Chrome, Firefox, Safari and Edge, so browsers use HTTPS for your domain even on the very first visit. To qualify, according to hstspreload.org, your domain must:
- Serve a valid certificate.
- Redirect HTTP to HTTPS on the same host (for example
http://example.com→https://example.com) if it listens on port 80. - Serve all subdomains over HTTPS, including
wwwif it has a DNS record, and internal subdomains. - Send an HSTS header on the base domain with
max-ageof at least31536000(one year),includeSubDomainsandpreload. HTTPS redirects must carry the header too.
Once the final stage has run without problems, add preload to the snippet, reload Nginx, and submit your domain on hstspreload.org:
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
Think twice before you preload. In the words of hstspreload.org, “inclusion in the preload list cannot easily be undone”. Removal is possible, but it takes months to reach users through browser updates. Every future subdomain, including internal ones, must support HTTPS from day one.
Nginx as a Reverse Proxy: Avoid Duplicate Headers
If the application behind Nginx (Node.js, a Java app server, another web server) sends its own HSTS header, visitors receive two Strict-Transport-Security headers, possibly with different values. In our test, a proxied backend with its own header produced exactly that. Browsers then only process the first header, so the policy you actually get depends on the order. Let Nginx own the header and hide the backend’s copy:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_hide_header Strict-Transport-Security;
}
How to Disable HSTS
Browsers keep the policy until max-age runs out, so you can’t simply delete the header. To withdraw it, keep HTTPS working and send max-age=0, which clears the policy in every browser that visits again:
add_header Strict-Transport-Security "max-age=0" always;
Browsers that don’t return keep the old policy until it expires. A preloaded domain also needs a removal request on hstspreload.org.
Common Mistakes
- Starting with a two-year
max-ageandincludeSubDomainsbefore checking every subdomain. Ramp up instead. - Forgetting
always, so error pages and some redirects go out without the header. - Adding
add_headerin a location and losing HSTS there (Step 3). - Sending the header only on port 80. Browsers ignore it over plain HTTP.
- Adding
preload“just in case”. It is a request to be hard-coded into browsers. Add it only when you intend to submit. - Putting HSTS in the
httpblock ofnginx.conf. Every server inherits it, including port 80 and sites that don’t use HTTPS, and any server block with its ownadd_headerdrops it. A snippet included in each HTTPS server is easier to reason about.
Conclusion
HSTS is a one-line change with a long memory. Put the header in a snippet with always, include it in every HTTPS server block and in every location that sets its own headers, start with a five-minute max-age and raise it to two years over about six weeks. Add preload only when every subdomain is ready for HTTPS for good.
Using Apache? See how to enable HSTS in Apache. For the redirect and TLS settings HSTS builds on, read our guide to redirect HTTP to HTTPS in Nginx.
How did it make you feel?
