https 站中的几大难题
性能,包括:
其次,兼容性及周边,如:
如何解决
关注安全与兼容性
其次是一篇关于 nginx 中 ssl 性能优化的文章《》。
密钥交换算法
常见的密钥交换算法有 rsa,ecdhe,dh,dhe 等算法。它们的特性如下:
建议优先支持 rsa 和 ecdh_rsa 密钥交换算法。原因是:
更改其配置如下(参照 nginx performance tuning for ssl( http://dojo.techsamurais.com/?p=1384 ):
1
2
3
4
5
|
ssl_protocols tlsv1 tlsv1.1 tlsv1.2; ssl_ciphers ecdhe-rsa-aes256-sha384:aes256-sha256:rc4:high:!md5:!anull:!enull:!null:!dh:!edh:!aesgcm; ssl_prefer_server_ciphers on; ssl_session_cache shared:ssl:10m; ssl_session_timeout 10m; |
或者
1
|
ssl_ciphers ecdhe-ecdsa-aes256-gcm-sha384:ecdhe-rsa-aes256-gcm-sha384:ecdhe-ecdsa-aes256-sha384:ecdhe-rsa-aes256-sha384:ecdhe-ecdsa-aes128-gcm-sha256:ecdhe-rsa-aes128-gcm-sha256:ecdhe-ecdsa-aes128-sha256:ecdhe-rsa-aes128-sha256:ecdhe-ecdsa-rc4-sha:!ecdhe-rsa-rc4-sha:ecdh-ecdsa-rc4-sha:ecdh-rsa-rc4-sha:ecdhe-rsa-aes256-sha:!rc4-sha:high:!anull:!enull:!low:!3des:!md5:!exp:!cbc:!edh:!kedh:!psk:!srp:!kecdh; |
(禁用了rc4,sha1,md5等算法)
辅助加速
启用spdy
spdy 是 google 推出的优化 http 传输效率的协议( https://www.chromium.org/spdy ), 它基本上沿用了 http 协议的语义, 但是通过使用帧控制实现了多个特性,显著提升了 http 协议的传输效率。
spdy 最大的特性就是多路复用,能将多个 http 请求在同一个连接上一起发出去,不像目前的 http 协议一样,只能串行地逐个发送请求。
可以在编译nginx带上参数 –with-http_spdy_module 支持 spdy 协议,然后可在配置中启用:
1
|
listen 443 ssl spdy; |
检测是否使用spdy的网址( https://spdycheck.org/ )
hsts
hsts(http strict transport security)。服务端返回一个 hsts 的 http header,浏览器获取到 hsts 头部之后,在一段时间内,不管用户输入 www.baidu.com 还是 http://www.baidu.com ,都会默认将请求内部跳转成 https://www.baidu.com;
将下述行添加到你的 https 配置的 server 块中:
1
|
add_header strict-transport-security "max-age=31536000"; |
session cache
session cache 的原理是使用 client hello 中的 session id 查询服务端的 session cache, 如果服务端有对应的缓存,则直接使用已有的 session 信息提前完成握手,称为简化握手。
session cache 有两个缺点:
session cache 也有一个非常大的优点:session id 是 tls 协议的标准字段,市面上的浏览器全部都支持 session cache。
1
2
|
ssl_session_cache shared:ssl:20m; ssl_session_timeout 20m; |
参照 nginx 的官方文档 1mb 内存大约可以存储 4000 个 session,按例配置 20m 大约可以存储 80000。根据需求合理设置。
ocsp stapling
ocsp 全称在线证书状态检查协议 (rfc6960),用来向 ca 站点查询证书状态,比如是否撤销。通常情况下,浏览器使用 ocsp 协议发起查询请求,ca 返回证书状态内容,然后浏览器接受证书是否可信的状态。 将证书保存下来,浏览器请求时候直接通过自己的服务器发送回去,防止验证服务器出问题,还能加快访问速度。
查看oscp验证服务器地址:
1
|
openssl x509 -in 1_test.qupeiyin.net_bundle.crt -text |
在输出的文字中找到 ocsp - uri: ,后面的 url 就是 oscp 的验证服务器地址。
请求 oscp 证书
1
2
3
4
|
openssl ocsp -noverify \ -issuer /certificate-path/trustchain.crt \ -cert /certificate-path/trustchain.crt \ -url |
不出意外会收到如下的结果
1
2
3
|
trustchain.crt: good this update: oct 18 17:59:10 2014 gmt next update: oct 18 23:59:10 2014 gmt |
如果出现 403 错误,那就需要在 header 请求头加上域名参数如 -header “host” “ocsp2.globalsign.com” ,没问题后就可以直接保存下来证书文件,完整的命令如下:
1
2
3
4
|
openssl ocsp -noverify -issuer 1_root_bundle.crt -cert 1_root_bundle.crt -url -header "host" "ocsp1.wosign.com" -text -respout ./stapling_file.ocsp |
将保存下来的 stapling_file.ocsp 证书添加到 nginx 的配置中,如下,nginx 中配置变成了这样子:
1
2
3
4
|
ssl_stapling on; ssl_stapling_verify on; ssl_stapling_file /stapling_file.ocsp; ssl_trusted_certificate /certificate-path/trustchain.crt; |
这样子重启 nginx 后就会生效,可以使用下面的命令测试生效结果:
1
|
echo quit | openssl s_client -connect blog.alphatr.com:443 -status 2> /dev/null | grep -a 17 'ocsp response:' | grep -b 17 'next update' |
看到 ocsp response status: successful 这样的字样就是成功了。
ocsp证书有效期很短,大概不到一个月,所以过段时间要更新 ocsp 证书,不然还是会验证失败。需要用脚本定时更新ocsp证书。
总结:(全部优化参数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
ssl on; ssl_certificate /data/www/ssl/ssl.crt; ssl_certificate_key /data/www/ssl/ssl.key; ssl_trusted_certificate /data/www/ssl/trustchain.crt; ssl_protocols tlsv1 tlsv1.1 tlsv1.2; ssl_ciphers ecdhe-rsa-aes256-sha384:aes256-sha256:rc4:high:!md5:!anull:!enull:!null:!dh:!edh:!aesgcm; ssl_prefer_server_ciphers on; ssl_stapling on; ssl_stapling_verify on; ssl_session_cache shared:ssl:10m; ssl_session_timeout 10m; add_header strict-transport-security "max-age=31536000"; resolver 223.5.5.5 223.6.6.6 valid=300s; resolver_timeout 10s; error_page 497 $host$request_uri; |
参数详解:
ssl on 开启ssl
ssl_certificate 对应单张证书
ssl_certificate_key 对应私钥
ssl_trusted_certificate 对应信任链(即startcom ssl或者positive ssl中需要附加到单张证书后面的那两张证书,可以独立出来)
ssl_protocols 支持的ssl协议标准(nginx默认参数为:ssl_protocols sslv3 tlsv1 tlsv1.1 tlsv1.2;)
ssl_ciphers (nginx默认参数为:ssl_ciphers high:!anull:!md5;)
ssl_prefer_server_ciphers on; 指定服务器密码算法在优先于客户端密码算法时,使用sslv3和tls协议。
error_page 497 https://$host$request_uri; 通过497错误将http转跳到https
如对本文有疑问, 点击进行留言回复!!
Spring Cloud Alibaba系列,nacos作为服务配置中心
网友评论