Navigate back to the homepage

SSL Labs 评分 A+ 的 nginx 配置

宋辰文
September 8th, 2015 · 1 min read

可惜我的博客并不支持 https,这是因为 GitHub Pages 和 GitCafe Pages 都不支持在自定义域名上使用 https 访问。

但是不久前我刚刚为 bither.net 做了配置,现在 bither.netQUALYS SSL Labs 上的评分已经是 A+ 了。当你访问 bither.net 时,地址栏上显示的小锁就说明了 https 的安全性。

而且这一切其实一分钱都不用花。

要使用 https 首先就需要证书,StartSSL 上有免费的证书提供,注册过程多少有些繁琐,不过已经有不少人提供了详细的注册方法说明,我在这里就不赘述了。

重要的是,有了 SSL 证书之后要怎么配置 nginx 服务器才能足够安全。QUALYS SSL Labs 提供了对服务器 https 安全级别的测试,在这个测试中达到 A 级以上基本就可以认为是足够安全的。

废话不多说了,下面是我使用的 nginx 配置文件,参考自 konklone.com 的配置。我把注释翻译成了中文,方便大家看懂。

1# 重定向 http 请求
2server {
3 listen 80;
4 server_name bither.net;
5 return 301 https://$host$request_uri;
6}
7
8server {
9 # 'spdy' 开启了对 SPDY 的支持
10 listen 443 ssl spdy;
11 server_name bither.net;
12
13 # 这里需要添加你自己的配置, 比如指向静态文件的路径, 或者将请求重定向给其它进程
14
15 # 存放证书文件的位置
16 ssl_certificate /path/to/unified.crt;
17 ssl_certificate_key /path/to/my-private-decrypted.key;
18
19 # 需要添加的 http headers
20 # Strict-Transport-Security: 让浏览器记住直接访问 https 的网址, 不再去 http 上重定向了。
21 # 这个一旦设置上将会很难更改, 如果要和 includeSubdomains 一起使用时更要慎重。
22 # max-age: 记住的时长, 单位是秒 (31536000 = 1 年)
23 # includeSubdomains: 所有子域名都强制使用 https 访问, 这个如果不确定千万别开。
24 # preload: 告诉浏览器可以预加载你的域名的 HSTS。
25 # 在这里提交你的域名来让浏览器预加载: https://hstspreload.appspot.com
26 add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
27
28 # 选择特定的加密方式, 来保证远期保密, 并且避免已知的漏洞。
29 # 只对于 IE8/XP 的用户有一点点小例外 (DES-CBC3-SHA)
30 # 这里有可能要根据 nginx 的版本来做调整
31 # 参考自: https://www.ssllabs.com/ssltest/analyze.html
32 ssl_prefer_server_ciphers on;
33 ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED';
34
35 # 不再使用旧的不安全的 SSLv2 和 SSLv3
36 ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
37
38 # 打开 Session 恢复, 缓存 10 分钟
39 # 来自 http://nginx.org/en/docs/http/configuring_https_servers.html
40 ssl_session_cache shared:SSL:10m;
41 ssl_session_timeout 10m;
42 keepalive_timeout 70;
43
44 # 一个 MTU 中缓冲 1400 bytes 的数据
45 # nginx 1.5.9+ ONLY
46 ssl_buffer_size 1400;
47
48 # SPDY header 压缩 (0 是不压缩, 9 压缩率最高速度最慢). 推荐选择 6
49 # 但是 header 压缩在 SPDY versions 1 - 3 中都是有缺陷的
50 # 所以我们先关闭它, 等有了支持 SPDY 4 的 nginx 版本后再打开
51 spdy_headers_comp 0;
52
53 # 为 DH 椭圆曲线预先生成一个 2048 bit 的随机参数, 如果不设置的话默认只有 1024 bits
54 # 使用 OpenSSL 用如下命令生成:
55 # openssl dhparam -outform pem -out dhparam2048.pem 2048
56 ssl_dhparam /path/to/dhparam2048.pem;
57
58 # 下面这一大段被我注释掉了, 打开 OCSP stapling 有效减少 CA 服务器的负担。
59 # OCSP stapling - means nginx will poll the CA for signed OCSP responses,
60 # and send them to clients so clients don't make their own OCSP calls.
61 # https://en.wikipedia.org/wiki/OCSP_stapling
62 #
63 # while the ssl_certificate above may omit the root cert if the CA is trusted,
64 # ssl_trusted_certificate below must point to a chain of **all** certs
65 # in the trust path - (your cert, intermediary certs, root cert)
66 #
67 # 8.8.8.8 and 8.8.4.4 below are Google's public IPv4 DNS servers.
68 # nginx will use them to talk to the CA.
69 # 以下是被我注释掉的配置
70 #ssl_stapling on;
71 #ssl_stapling_verify on;
72 #resolver 8.8.8.8 8.8.4.4 valid=86400;
73 #resolver_timeout 10;
74 #ssl_trusted_certificate /path/to/all-certs-in-chain.crt;
75}

使用这个配置文件时有几点需要注意的地方:

  1. add_header Strict-Transport-Security 时一定要考虑好是不是要对所有的子域名都开启,如果子域名的服务器还没准备好接受 https 的请求的话,那一旦开启再想反悔就难了。
  2. ssl_ciphers 在设置时可能会因为操作系统和 nginx 版本不同而导致有的加密方式没办法开启,尽量升级最新的 nginx 吧,如果不方便升级的话就只能自行取舍了。

More articles from 宋辰文

知乎专栏 RSS 输出

知乎专栏的文章质量很高,可惜没有 RSS 输出,没办法整合到我常用的阅读器里。 于是我业余时间自己写了一个知乎专栏的 RSS 输出,并且自带聚合功能,可以在一个 Feed 里聚合所有自己喜欢的专栏的内容。

September 7th, 2015 · 1 min read

自动下载并更换 OS X 壁纸

虽然在工作中很少能看到壁纸,但我还是希望我的壁纸能经常变一变,这样当四指划开显示桌面时,说不定还能给自己一个惊喜。

June 6th, 2015 · 1 min read
© 2019 宋辰文
Link to $https://mp.weixin.qq.com/s?__biz=MzAxNTI3MTUwMA==&mid=2247483701&idx=2&sn=34613cbb3252ac5eb6c31724f3109b59Link to $https://weibo.com/songchenwenLink to $https://twitter.com/songchenwenLink to $https://stackoverflow.com/users/2210682/songchenwen