2

给博客加上HTTPS

 2 years ago
source link: https://www.qikqiak.com/post/make-https-blog/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

谁都不愿意在使用网站服务的时候,被恶心的运营商劫持加上一些他们的服务(真的很贱,不是吗?),不过这能难道我们程序员吗?当然不能,上https,老子全站https,你再劫持给我看看。

https证书服务大部分都是收费的,而且很贵,阿里云可以申请一个免费的证书,只能绑定一个域名,这里我们使用更加友好的免费https服务:Let’s Encrypt

Let’s Encrypt 简介

如果要启用HTTPS,我们就需要从证书授权机构(以下简称CA) 处获取一个证书,Let's Encrypt 就是一个 CA。我们可以从Let's Encrypt 获得网站域名的免费的证书。这篇文章也主要讲的是通过 Let's Encrypt + Nginx 来让网站升级到HTTPS

CertbotLet's Encrypt官方推荐的获取证书的客户端,可以帮我们获取免费的Let's Encrypt 证书。安装命令:

yum install certbot

生成证书:

certbot certonly --email [email protected] --agree-tos --webroot -w /var/www/blogs -d blog.qikqiak.com

其中-w后面是网站根目录,-d后面是网站域名,所以需要保证目录存在并且可以正常访问。

执行完生成证书的命令后,会生成相关的证书文件到/etc/letsencrypt/live/blog.qikqiak.com/目录下面:

$ ls /etc/letsencrypt/live/blog.qikqiak.com/
cert.pem  chain.pem  fullchain.pem  privkey.pem  README

至此证书生成完成。

Nginx 配置https

证书生成完成后,还需要更改我们的Nginx配置服务,主要是监听443端口,启用SSL,并配置SSL的证书路径(公钥,私钥的路径)。如下:

server
{
    listen       443;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/blog.qikqiak.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/blog.qikqiak.com/privkey.pem;
    server_name blog.qikqiak.com;
    root /home/notes/apps/blog;
    index index.html;
}

配置更改过后重新加载Nginx即可生效:

nginx -s reload

现在我们访问https://blog.qikqiak.com已经可以正常访问了,但是还有一个问题就是访问http://blog.qikqiak.com依然还是http的,所以我们需要配置将http更改为https,这样就完美了。这里可以用到之前我们的301跳转来解决这个问题,完整的配置如下:

server {
    listen  80;
    server_name blog.qikqiak.com;
    rewrite ^(.*)$  https://$host$1 permanent;
}

server
{
    listen       443;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/blog.qikqiak.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/blog.qikqiak.com/privkey.pem;
    server_name blog.qikqiak.com;
    root /home/notes/apps/blog;
    index index.html;
}

到这里我们已经将博客https化了,但是还没有完的,由于Let's Encrypt证书的有效期只有90天,所以在到期之前我们需要更新整数,certbot给我们已经提供了这样的更新命令,我们只需要将更新命令添加到crontab下面定期更新即可:

$ crontab -l
30 5 1 * * root /usr/bin/certbot renew --renew-hook "/usr/sbin/nginx -s reload"

到这一步才算革命成功了~~~ 我们再也不用担心恶心的害虫了~~~

微信公众号

扫描下面的二维码关注我们的微信公众帐号,在微信公众帐号中回复◉加群◉即可加入到我们的 kubernetes 讨论群里面共同学习。

wechat-account-qrcode

「真诚赞赏,手留余香」


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK