This is a text-only version of the following page on https://raymii.org: --- Title : HTTP Strict Transport Security for Apache, NGINX and Lighttpd Author : Remy van Elst Date : 17-06-2016 URL : https://raymii.org/s/tutorials/HTTP_Strict_Transport_Security_for_Apache_NGINX_and_Lighttpd.html Format : Markdown/HTML --- HTTP Strict Transport Security (often abbreviated as HSTS) is a security feature that lets a web site tell browsers that it should only be communicated with using HTTPS, instead of using HTTP. This tutorial will show you how to set up HSTS in Apache2, NGINX and Lighttpd. It is tested with all mentioned webservers, NGINX 1.1.19, Lighttpd 1.4.28 and Apache 2.2.22 on Ubuntu 12.04, Debian 6 & 7 and CentOS 6.It should work on other distro's however, these are just reference values.
### What is HTTP Strict Transport Security? Quoting the [Mozilla Developer Network][2]: If a web site accepts a connection through HTTP and redirects to HTTPS, the user in this case may initially talk to the non-encrypted version of the site before being redirected, if, for example, the user types http://www.foo.com/ or even just foo.com. This opens up the potential for a man-in-the-middle attack, where the redirect could be exploited to direct a user to a malicious site instead of the secure version of the original page. The HTTP Strict Transport Security feature lets a web site inform the browser that it should never load the site using HTTP, and should automatically convert all attempts to access the site using HTTP to HTTPS requests instead. An example scenario: You log into a free WiFi access point at an airport and start surfing the web, visiting your online banking service to check your balance and pay a couple of bills. Unfortunately, the access point you're using is actually a hacker's laptop, and they're intercepting your original HTTP request and redirecting you to a clone of your bank's site instead of the real thing. Now your private data is exposed to the hacker. Strict Transport Security resolves this problem; as long as you've accessed your bank's web site once using HTTPS, and the bank's web site uses Strict Transport Security, your browser will know to automatically use only HTTPS, which prevents hackers from performing this sort of man-in-the-middle attack. _Do note that HSTS does not work if you've never visited the website before._ A website needs to tell you it is HTTPS only. ### Important regarding preload In the below configuration the `preload` directive was used. As requested by Lucas Garron from Google I removed it since most people seem to do screw it up. Please note that that `THE PRELOAD DIRECTIVE WILL HAVE SEMI-PERMANENT CONSEQUENCE`. If you are testing, screw up or don't want to use HSTS anymore you might be on the preload list. It is important that you understand what you are doing and that you understand that the preload directive means that it will end up in browsers. If your HTTPS configuration is wrong, broken or you don't want to use HTTPS anymore, you will experience problems. [See this page][3] as well. If you still want to use `preload`, just append it to the header after the semi- colon. ### Set up HSTS in Apache2 Edit your apache configuration file (`/etc/apache2/sites-enabled/website.conf` and `/etc/apache2/httpd.conf` for example) and add the following to your VirtualHost: # Optionally load the headers module: LoadModule headers_module modules/mod_headers.so