个人感觉如果是个个人博客网站没必要添加ssl证书,但chrome放出狠话,如果不加ssl,将会提示不安全网站,没办法,网上找找解决办法,给加上了
介绍
Let’s Encrypt是一个免费并且开源的CA,且已经获得Mozilla、微软等主要浏览器厂商的根授信。它极大低降低DV证书的入门门槛,进而推进全网的HTTPS化。
Certbot is an easy-to-use client that fetches a certificate from Let’s Encrypt—an open certificate authority launched by the EFF, Mozilla, and others—and deploys it to a web server.
本文所有的操作均在Ubuntu16.04下进行安装和配置。
安装
本文直接使用 Let’s Encrypt 官网推荐的自动部署脚本Certbot。
根据环境选择,我们选择Nginx+Ubuntu16.04。
如果已经安装python和Nginx,使用如下方法:
sudo apt-get update
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot如果没有安装,安装方法如下:
$ sudo apt-get update
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx
接下来,我们要生成自己的证书。
获取证书前需要先停止 Nginx 服务
service nginx stop生成单域名证书(可以一次性生成多个单域名证书)
certbot certonly --standalone --email your@email.com -d yourdomain.com -d www.yourdomain.com到此,如果没有什么意外,执行完命令之后,你就可以看到你的证书创建成功的提示!默认是在 /etc/letsencrypt/live 路径下
Nginx配置
有了证书,接下来我们就可以配置Nginx了。
进入Nginx的配置文件夹(/etc/nginx/sites-available/),创建一个ssl.conf配置文件,在里面增加一个server配置。
配置的内容,基本和监听http的配置相似,主要的区别是监听443端口和证书的加载,一个例子如下:
server {
# SSL configuration
listen 443 ssl;
listen [::]:443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
server_name yourdomain.com www.yourdomain.com;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
try_files $uri $uri/ =404;
}
}保存,然后重启nginx:
servcie ngxin start这时候我们就可以在域名前加上https,就可以发现成功了!
仅限Https访问
有了Https,我们一般也就不想要http的访问了,或者说想把所有的http请求转为https。方法有很多种,我的方法是使用rewrite,把原先的Http全部转化为Https。
一个例子如下:
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
rewrite ^(.*) https://$server_name$1 permanent;
}自此,网站的Https加密工作完成。