搭建个人博客的时候,就看过一些免费证书申请的文章,也浏览过一些使用Https的博客。使用Https要比Http安全很多,至少在国内很普通的流量劫持攻击会减少很多。闲来无事,测试把自己的个人博客也升级到Https。
使用Https加密首先就需要申请个人博客证书,收费的证书颁发机构国内和国外都有很多。个人博客推荐使用现在比较流行的Let's Encrypt。申请过程比较简单,使用官方自动化脚本:
1、配置git环境:# yum install git
2、下载自动化脚本到本地
# git clone https://github.com/certbot/certbot
# cd certbot
3、生成证书
./certbot-auto certonly --standalone --email loryxxx@hotmail.com -d iorisun.com -d www.iorisun.com
将安装以下软件包
安装过程,需要手动确认是否安装,输入Y确认。另外还需要接受官方协议,输入A回车即可。
注意:申请时,防火墙需要开通https端口
# firewall-cmd --permanent --zone=public --add-service=https
# firewall-cmd --reload
申请成功后,有以下提示:
可通过tree命令查看:# tree /etc/letsencrypt/live/
4、修改nginx配置文件
# vim /etc/nginx/conf.d/default.conf
在Nginx配置文件中加入SSL参数定义,把http重定向到https
如下:修改和增加以下内容(为避免升级nginx后主页被修改,最好把index.php放在首位)
重启服务后,再打开网站就可以看到绿色的小锁了。
5、设置证书自动延期
Let's Encrypt的证书有效期只有90天,需要定期更新证书:
通过crontab制定一个计划任务,每2个月(即1、3、5、7、9、11月15日凌晨2点0分)更新一次。
0 2 15 */2 * root /root/certbot/certbot-auto renew --standalone --pre-hook "service nginx stop" --post-hook "service nginx start" --force-renewal
或者使用./certbot-auto renew -v命令也可以(注:要先关闭nginx服务再运行,否则会报错)。
6、修改博客站点地址
注意:需要同步修改图片地址http为https(如修改数据库等)
优化Https安全性:
1、生成dhparam.pem
# cd /etc/ssl/certs/
# openssl dhparam -out dhparam.pem 2048
2、生成HTTP Public Key Pinning(https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# cd /etc/letsencrypt/live/iorisun.com/ # openssl rsa -in privkey.pem -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64 writing RSA key 9rcncxVnzws62MPkX5nUJPKs5jAGAhnxeqANv0Hx7Vo= 或者: # openssl s_client -servername www.iorisun.com -connect www.iorisun.com:443 | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64 depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3 verify return :1
depth=1 C = US, O = Let 's Encrypt, CN = Let' s Encrypt Authority X3
verify return :1
depth=0 CN = iorisun.com verify return :1
writing RSA key 9rcncxVnzws62MPkX5nUJPKs5jAGAhnxeqANv0Hx7Vo= # openssl x509 -in chain.pem -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64 writing RSA key YLh1dUR9y6Kja30RrAn7JKnbQG /uEtLMkBgFF2Fuihg =
生成备用证书指纹(官方备用): # curl https://letsencrypt.org/certs/lets-encrypt-x4-cross-signed.pem | openssl x509 -pubkey | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | base64 % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1647 100 1647 0 0 1968 0 --:--:-- --:--:-- --:--:-- 1967 sRHdihwgkaib1P1gxX8HFszlD+7 /gTfNvuAybgLPNis =
|
3、修改nginx配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# vim /etc/nginx/conf.d/default.conf server { listen 80;
listen 443 ssl http2;
server_name www.iorisun.com iorisun.com;
root /usr/share/nginx/html/ ;
index index.html index.php index.htm;
if ($scheme = http) {
return 301 https: // $server_name$request_uri;
}
ssl_certificate /etc/letsencrypt/live/iorisun .com /fullchain .pem;
ssl_certificate_key /etc/letsencrypt/live/iorisun .com /privkey .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" ;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/iorisun .com /chain .pem;
ssl_dhparam /etc/ssl/certs/dhparam .pem;
server_tokens off;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
fastcgi_hide_header X-Powered-By;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" ;
add_header Public-Key-Pins 'pin-sha256="9rcncxVnzws62MPkX5nUJPKs5jAGAhnxeqANv0Hx7Vo="; pin-sha256="YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg="; pin-sha256="sRHdihwgkaib1P1gxX8HFszlD+7/gTfNvuAybgLPNis="; max-age=2592000; includeSubDomains' ;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
|
配置完之后可以测试一下网站的安全级别:https://www.ssllabs.com/ssltest/
参考:https://blog.kuoruan.com/71.html
https://imququ.com/post/http-public-key-pinning.html
https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
https://ithenrik.com/blog/posts/activating-http-public-key-pinning-hpkp-on-lets-encrypt