nginx、Apache、Lighttpd启用HSTS

默北 web应用231,1178字数 1507阅读5分1秒阅读模式

302跳转

通常情况下,我们将用户的 HTTP 请求 302 跳转到 HTTPS,这会存在两个问题:

  1. 不够安全,302 跳转会暴露用户访问站点,也容易被劫持
  2. 拖慢访问速度,302 跳转需要一个 RTT(The role of packet loss and round-trip time),浏览器执行跳转也需要时间

HSTS

302 跳转是由浏览器触发的,服务器无法完全控制,这个需求导致了 HSTS(HTTP Strict Transport Security)的诞生。HTSP 就是添加 header 头(add_header Strict-Transport-Security max-age=15768000;includeSubDomains),告诉浏览器网站使用 HTTPS 访问,支持HSTS的浏览器(Chrome, firefox, ie 都支持了 HSTS(http://caniuse.com/#feat=stricttransportsecurity))就会在后面的请求中直接切换到 HTTPS。在 Chrome 中会看到浏览器自己会有个 307 Internal Redirect 的内部重定向。在一段时间内也就是max-age定义的时间,不管用户输入www.ttlsa.com还是https://www.ttlsa.com,都会默认将请求内部跳转到https://www.ttlsa.com。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

服务器端配置HSTS,减少302跳转,其实HSTS的最大作用是防止302 HTTP劫持。HSTS的缺点是浏览器支持率不高,另外配置HSTS后HTTPS很难实时降级成HTTP。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

同时,也建议启用SPDY来提高性能。有关SPDY内容参见前面文章,不在此外累述了。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

下面来说说如何在Apache2, NGINX , Lighttpd启用HSTS。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

Apache2

# 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服务。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

nginx

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

在server端添加该头部,并重启服务。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

Lighttpd

server.modules += ( "mod_setenv" )
$HTTP["scheme"] == "https" {
    setenv.add-response-header  = ( "Strict-Transport-Security" => "max-age=63072000; includeSubdomains; preload")
}

X-Frame-Options 头部

X-Frame-Options 头部添加到HTTPS站点,确保不会嵌入到frame 或 iframe,避免点击劫持,以确保网站的内容不会嵌入到其他网站。文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/

Apache

Header always set X-Frame-Options DENY

nginx

add_header X-Frame-Options "DENY";

Lighttpd

server.modules += ( "mod_setenv" )
$HTTP["scheme"] == "https" {
    setenv.add-response-header  = ( "X-Frame-Options" => "DENY")
}
文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/文章源自运维生存时间-https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 23/07/2015 01:00:01
  • 转载请务必保留本文链接:https://www.ttlsa.com/web/hsts-for-nginx-apache-lighttpd/
  • Apache
  • HSTS
  • Lighttpd
  • nginx
  • spdy
评论  2  访客  2
    • 私立碧阳学园学生会
      私立碧阳学园学生会 0

      Lighttpd设置好了以后,只有狐狸能自动从HTTP跳转到HTTPS,IE 11和Chrome 48.0都不行。

    评论已关闭!