How to set an HSTS policy in common web servers

The basic problem that HSTS solves is that even after a website turns on HTTPS, visitors may still end up trying to connect over plain HTTP. For example:
  • When a user types “” into the URL bar, browsers default to using http://.
  • A user may click on an old link that mistakenly uses an http:// URL.
  • A user’s network may be hostile and actively rewrite https:// links to http://.
Websites that prefer HTTPS will generally still listen for connections over HTTP in order to redirect the user to the HTTPS URL. For example:
$ curl --head

HTTP/1.1 301 Moved Permanently
This redirect is insecure and is an opportunity for an attacker to capture information about the visitor (such as cookies from a previous secure session), or to maliciously redirect the user to a phishing site.
This can be addressed by returning a Strict-Transport-Security header whenever the user connects securely. For example:
$ curl --head

HTTP/1.1 200 OK
Strict-Transport-Security: max-age=15552000; preload
This enables HSTS for While HSTS is in effect, clicking any links to will cause the browser to issue a request directly for
In the above example, the browser will remember the HSTS policy for 180 days. The policy is refreshed every time browser sees the header again, so if a user visits at least once every 180 days, they’ll be indefinitely protected by HSTS.

HSTS Preloading

Since it’s just an HTTP header, HSTS is very easy to add to a domain.
However, to enable HSTS for a domain via the HTTP header, the browser does have to see the header at least once. This means that users are not protected until after their first successful secure connection to a given domain.
To solve the “first visit” problem, the Chrome security team created an “HSTS preload list”: a list of domains baked into Chrome that get Strict Transport Security enabled automatically, even for the first visit. Firefox, Safari, and newer versions of Internet Explorer also incorporate Chrome’s HSTS preload list.
The Chrome security team allows any domain to submit their domain to the list, provided it meets the following requirements:
  • HTTPS is enabled on the root domain (e.g., and all subdomains (e.g. – especially the www subdomain, if a DNS record for it exists.
  • The HSTS policy includes all subdomains, with a long max-age, and a preload flag to indicate that the domain owner consents to preloading.
  • The website redirects from HTTP to HTTPS, at least on the root domain.
An example of a valid HSTS header for preloading:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
While you can’t hardcode the entire internet into a big list, the HSTS preload list is a simple, effective mechanism for locking down HTTPS for the near future. As the web transitions fully to HTTPS over the long-term, and browsers can start phasing out plain HTTP and defaulting to HTTPS, the HSTS preload list (and HSTS itself) may eventually become unnecessary.

HSTS as a forcing function

Strict Transport Security provides meaningful security benefits to visitors, especially visitors on hostile networks.
However, it’s also highly valuable as an organizational forcing function and compliance mechanism.
When a domain owner follows the recommendations in this article and sets an HSTS policy on its base domain with includeSubDomains and preload, the domain owner is saying “Every part of our web infrastructure is HTTPS, and always will be.” — and is giving browsers permission to vigorously enforce that from then onwards.
It’s a clear and auditable commitment, and gives anyone overseeing an organization’s transition to HTTPS a way of marking domains as “done”.
Zooming out even further: it’s technically possible to preload HSTS for an entire top-level domain, as Google first did with .google

Configuration for common web servers

On nginx, you would apply an add_header command to the appropriate virtual host configuration:
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
On Apache, you would apply a Header directive to always set the HSTS header, like so:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
On Microsoft systems running IIS (Internet Information Services), there are no “.htaccess” files to implement custom headers. IIS applications use a central “web.config” file for configuration. For IIS 7.0 and up, the code is as follows:
<add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains; preload "/>



Popular posts from this blog

Check MySQL query history from command line