各服务器推荐配置可以参考:https://cipherli.st/

为了确保更强的安全性,采取了迪菲-赫尔曼密钥交换.

/etc/ssl/certs目录下生成dhparam.pem:

cd /etc/ssl/certs
openssl dhparam -out dhparam.pem 2048

如果服务器配置好,可以选择4096bit加密方式
生成之后,在nginx配置中添加

ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers “EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4”;
keepalive_timeout 70;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

生成HTTP Public Key Pinning

openssl rsa -in /etc/letsencrypt/live/yalv.me/privkey.pem -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64
writing RSA key
FankyFNJUnHYf737yBalLqkC8bWTxZGTCeoMezFgNX0=
 
openssl x509 -in /etc/letsencrypt/live/yalv.me/chain.pem -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
writing RSA key
YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=

加上以下几个增强安全性的命令

#减少点击劫持
add_header X-Frame-Options DENY;
#禁止服务器自动解析资源类型
add_header X-Content-Type-Options nosniff;
#防XSS攻击
add_header X-Xss-Protection 1;

隐藏服务器版本号信息

由于某些 Nginx 漏洞只存在于特定的版本,隐藏版本号可以提高安全性。
在配置中加入
server_tokens off;

推荐配置

下面是我的博客配置:

server
    {
listen 80;
server_name yalv.me;
return 301 https://$host$request_uri;
    }
server
    {
listen 443 ssl;
server_name yalv.me;
index index.html index.htm index.php default.html default.htm default.php;
root  /home/wwwroot/yalv;
 #证书文件
ssl_certificate /etc/letsencrypt/live/yalv.me/fullchain.pem;
 #私钥文件
ssl_certificate_key /etc/letsencrypt/live/yalv.me/privkey.pem;
 #优先采取服务器算法
 ssl_prefer_server_ciphers on;
 #使用DH文件        
ssl_dhparam /etc/ssl/certs/dhparams.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 #定义算法
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
 #设置长连接
keepalive_timeout 70;
 #配置共享会话缓存大小,视站点访问情况设定
ssl_session_cache shared:SSL:10m;
 #配置会话超时时间
ssl_session_timeout 10m;

ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/yalv.me/chain.pem;
ssl_stapling on;
 #隐藏服务器版本号信息
server_tokens off;
 #HTTP Public Key Pinning
add_header Public-Key-Pins 
pin-sha256="YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg="; 'pin-sha256="yzWArufCvbckI2ChQnH44zpw3omhXPVK5XiQHBgq8vU="; max-age=2592000; includeSubDomains';
 #减少点击劫持
add_header X-Frame-Options DENY;
 #禁止服务器自动解析资源类型
add_header X-Content-Type-Options nosniff;
 #防XSS攻擊
 add_header X-Xss-Protection 1;
}

测试站点的安全级别

配置完之后可以测试一下现在的安全级别:https://www.ssllabs.com/ssltest
https-leve

2017.03.17更新:
发现一个有趣的网站,可以根据你的服务器,来自动生成conf 配置:点击这里