一、Apache HSTS 部署
#启用HTTP严格传输安全
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
#HSTS策略只能在加密通道的HTTP响应中进行设置,因此需要把http重定向到https,如果明文响应中允许设置HSTS头,中间人攻击者就可以通过在普通站点中注入HSTS信息来执行DoS攻击;
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
…
#将所有访问者重定向到加密的网站
RedirectPermanent / https://www.example.com/
<VirtualHost>
给大家一个简单的示例代码:
# Optionally load the headers module:
LoadModule headers_module modules/mod_headers.so
<VirtualHost 0.0.0.0:443>
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload
</VirtualHost>
二、Apache HPKP 部署
下面的例子固定到 COMODO RSA Domain Validation Secure Server CA 及备份的 Comodo PositiveSSL CA 上,30天失效期,包括所有的子域。
编辑你的 Apache 配置文件(如 /etc/apache2/sites-enabled/website.conf 或 /etc/apache2/httpd.conf),并添加下列行到你的 VirtualHost 中:
# 如需要,载入 headers 模块。
LoadModule headers_module modules/mod_headers.so
Header set Public-Key-Pins "pin-sha256=\"klO23nT2ehFDXCfx3eHTDRESMz3asj1muO+4aIdjiuY=\"; pin-sha256=\"633lt352PKRXbOwf4xSEa1M517scpD3l5f79xMD9r9Q=\"; max-age=2592000; includeSubDomains"