为何要用 https?
https 更安全。甚至为了安全,一个专业可靠的网站, https 是必须的。 Firefox 和 Chrome 都计划将没有配置 SSL 加密的 https 网站标记为不安全(貌似 Firefox50 已经这么干了),目前它们也正在联合其余相关的基金会与公司推进整个互联网 https 化,如今你们访问的一些主要的网站。如 Google 多年前就已经所有启用 https ,国内的淘宝、搜狗、知乎、百度等等也全面 https 了。甚至 Google 的搜索结果也正在给予 https 的网站更高的排名和优先收录权。nginx
要须要掌握哪些知识?
拥有基础的 Linux 系统知识,经常使用的 Shell 命令。git
须要有什么资源?
若是你要在生产环境使用 https,请接着往下看:docker
一个服务器,一个域名,而且已经正确的将域名指向了你的服务器,而且拥有服务器的 SSH 权限小程序
Tips: 须要注意的是若是是在国内阿里云或者腾讯云购买的域名请备案,不然即便你正确的配置了你的域名,可能也依然没法访问你的服务器,由于在国内备案是必须的。浏览器
关键词
简单来讲,http 是一个传输网页内容的协议,好比你看到的 http 开头的网站 http://www.163.com,其网页上的文字、图片、 CSS、JS 等文件都是经过 http 协议传输到咱们的浏览器,而后被咱们看到。安全
而 https 能够理解为 HTTP over SSL/TLS ,好端端的 http 为何须要 over SSL/TLS 呢,由于 http 是明文传输的,经过 http 协议传输的内容很容易被偷看和篡改,为了安全(你确定不想被人偷看或者篡改网页内容吧,好比网站银行密码什么的。)就为 http 协议再加上了一层 SSL/TLS 安全协议,因此就有了 https。服务器
HTTP over SSL/TLS 字面意思就是带 安全套接层 的 http 协议,心里纯洁的同窗也能够理解为 带安全套的 http,由于带了安全套,因此固然会比较安全(/(ㄒoㄒ)/~~)。其中 SSL 是 Secure Sockets Layer 的缩写,是 “安全套接层” 的意思。TLS 是 Transport Layer Security 的缩写,是 传输层安全协议 的意思。SSL 和 TLS 是同一个东西的不一样阶段,理解为同一个东西也行,都是安全协议就对了。
证书颁发机构(CA, Certificate Authority)即颁发数字证书的机构。是负责发放和管理数字证书的权威机构,并做为电子商务交易中受信任的第三方,承担公钥体系中公钥的合法性检验的责任。
Let's Encrypt 成立于 2015 年的数字证书认证机构,成立的目的就是为了推行 https 协议在全世界的普及。主要赞助商包括电子前哨基金会、Mozilla基金会、Akamai以及思科。Let's Encrypt 旨在以自动化流程消除手动建立和安装证书的复杂流程,并推广使万维网服务器的加密链接无所不在,为安全网站提供免费的 SSL/TLS 证书。
说人话就是 Let’s Encrypt 就是一个能够给咱们颁发授信证书的 CA 组织。
Let’s Encrypt 官方推出的授信证书生成客户端。能够在 Debian,CentOS 等主流系统上运行
要升级到 https 首先要得到 CA 签发的安全证书文件,咱们选择的证书签发机构是 Let’s Encrypt,目前 Let’s Encrypt 推荐的方法是安装 Certbot 工具经过校验服务器和域名的归属权方式来获取证书
安装Certbot
若是你的英文能力比较好,那么请访问 Certbot官网 根据官方教程来下载,官方教程更加细致,出了问题也方便查看官方的第一手资料。固然你也能够根据如下命令来安装 Certbot 工具,这里咱们以 debian9 系统为例来安装
# 下载 certbot,若是是 centOS 等内核,可使用 yum 命令 apt install certbot
生成证书
certbot 安装成功后,咱们有两种模式来获取证书
certbot certonly --webroot -w /root/docker_nginx/html -d liluyang.me -d www.liluyang.me
这个命令会为 liluyang.com 和 www.liluyang.com 这两个域名生成一个证书
--webroot 模式会在 /root/docker_nginx/html 中建立 .well-known 文件夹,这个文件夹里面包含了一些验证文件,certbot 会经过访问 example.com/.well-known/acme-challenge 来验证你的域名是否绑定的这个服务器。这个文件夹就是你 Nginx 下 index.html 所在的目录,请自行根据本身服务器 Nginx 的配置状况修改这个参数。
若是执行成功,命令行会大体输出如下参数表示成功:
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/liluyang.me/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/liluyang.me/privkey.pem Your cert will expire on 2019-07-01. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
以上信息中包含了生成后的证书的位置,过时时间等信息,根据提示信息咱们打开 /etc/letsencrypt/live/ 文件夹,检查这个文件夹中是否有你生成的对应网站的证书文件,若是成功,应该以下图所示:
这个命令在大多数状况下均可以知足需求,若是失败了,请看下一种模式。
若是你已经经过第一种模式生成证书成功,请跳过这里,直接看下一章
可是有些时候咱们的一些服务并无根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另一种模式 --standalone, 这种模式不须要指定网站根目录,他会自动启用服务器的 443 端口,来验证域名的归属。咱们有其余服务(例如nginx)占用了 443 端口,就必须先中止这些服务,在证书生成完毕后,再启用。
certbot certonly --standalone -d example.com -d www.example.com
证书生成完毕后,咱们能够在 /etc/letsencrypt/live/ 目录下看到对应域名的文件夹,里面存放了指向证书的一些快捷方式。
这时候咱们的第一辈子成证书已经完成了,接下来就是配置咱们的 web 服务器,启用 HTTPS。
若是生成失败,而且你本身搜索以后又没有能力解决,请在评论留言
若是生成失败,而且你本身搜索以后又没有能力解决,请在评论留言
若是生成失败,而且你本身搜索以后又没有能力解决,请在评论留言
证书生成生成后,咱们还要作最后的一步就是把证书的配置信息添加到 nginx 的 conf 目录的 certs 文件夹中。我我的的 Nginx 是经过 docker 镜像的方式运行的,而且我将本地的 /root/docker_nginx/conf 文件夹设置为了 Nginx 的配置文件夹,因此如今咱们将证书 cert.pem 和 privkey.pem 拷贝到这个目录。配置目录以下
在 Nginx 的配置文件 nginx.conf 末尾添加如下配置,注意修改对应的配置参数,下面的配置模板仅供参考。
若是存在 conf.d/default.conf 文件请优先修改此文件,具体缘由请百度
# 443 https 配置 server { listen 443 ssl http2; server_name liluyang.me www.liluyang.me; ssl on; ssl_certificate /etc/nginx/certs/cert.pem; ssl_certificate_key /etc/nginx/certs/privkey.pem; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/html; index index.html index.htm; } }
重启 Nginx
到此为止,咱们就已经成功的完成了 https 的配置。 如今打开个人博客 https://liluyang.com 就能够在浏览器端看到标有 安全 的网站标识字样了,以下图:
若是是 docker 部署的 nginx,最后重启启动 docker 时要将 443(https) 端口映射出来,不然没法访问服务器的 443(https) 端口的。
docker run \ --volume "$PWD/html":/usr/share/nginx/html \ --volume "$PWD/conf":/etc/nginx \ -p 80:80 \ -p 443:443 \ -d \ nginx
强制使用 https 访问网站
将 http 请求转发到 https
请在配置文件夹中添加如下配置,而后重启 Nginx 便可
server { listen 80; server_name liluyang.me www.liluyang.me; return 301 https://liluyang.me; }
自动续订
细心的同窗可能会发现, https 证书的有效时间是很是短的,只有三个月,那么每隔三个月咱们都要操做一次吗?其实不是的,咱们能够用过自动续订的方式来刷新咱们的证书有效时间。
certbot renew --dry-run
若是测试成功,会生成以下提示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/liluyang.me.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator webroot, Installer None Renewing an existing certificate Performing the following challenges: http-01 challenge for liluyang.me http-01 challenge for www.liluyang.me Waiting for verification... Cleaning up challenges - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed without reload, fullchain is /etc/letsencrypt/live/liluyang.me/fullchain.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/liluyang.me/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal.
certbot renew -v
自动续订也能够用 certbot 来实现,自动续订以后会更新 pem 文件到 /etc/letsencrypt/live/example.com/ 。若是你的 nginx 配置没有直接指向 /etc/letsencrypt/live/example.com/ 文件夹,请记得将 pem 文件夹拷贝到你的配置文件夹中
certbot renew --quiet --no-self-upgrade
广告:扫码关注 知乎日报 小程序客户端
原文链接:http://www.noobyard.com/article/p-vbmxtrbf-et.html














