nginx Apache Lighttpd 启用 HSTS

栏目: 服务器 · 发布时间: 7年前

内容简介:nginx Apache Lighttpd 启用 HSTS

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还是http://www.ttlsa.com,都会默认将请求内部跳转到https://www.ttlsa.com。

服务器端配置HSTS,减少302跳转,其实HSTS的最大作用是防止302 HTTP劫持。HSTS的缺点是浏览器支持率不高,另外配置HSTS后HTTPS很难实时降级成HTTP。

同时,也建议启用SPDY来提高性能。有关SPDY内容参见前面文章,不在此外累述了。

下面来说说如何在Apache2,NGINX , Lighttpd启用HSTS。

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服务。

nginx

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

在server端添加该头部,并重启服务。

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,避免点击劫持,以确保网站的内容不会嵌入到其他网站。

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")
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 3

The Art of Computer Programming, Volume 3

Donald E. Knuth / Addison-Wesley Professional / 1998-05-04 / USD 74.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 3》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具