CentOS7中Nginx免费开启https

名词解释

HTTPS(超文本传输安全协议)是一种互联网通信协议,可保护用户计算机与网站之间传输的数据的完整性和机密性。用户在访问网站时都希望获得安全私密的在线体验。

Lets Encrypt 是由 Internet Security Research Group (ISRG) 开发的免费开放的证书颁发机构。Lets Encrypt 颁发的证书如今几乎得到所有浏览器的信任。

前提条件

  • 你有一个指向你的公共服务器 IP 的域名。在本教程中,我们将使用rumenz.com.
  • 你已经安装 Nginx

安装Certbot

Certbot 是一个易于使用的工具,可以自动执行获取和更新 Lets Encrypt SSL 证书以及配置 Web 服务器的任务。

要从 EPEL 存储库安装 certbot 包 yum install certbot

生成 Dh (Diffie-Hellman)

Diffie–Hellman (DH)密钥交换 是一种通过不安全的通信通道安全地交换加密密钥的方法。通过键入以下命令生成一组新的 2048 位 DH 参数:

  1. > openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

你可以将大小更改为 4096 位,但在这种情况下,生成可能需要 30 分钟以上,具体取决于系统熵。

获取 Lets Encrypt SSL 证书

要为我们的域获取 SSL 证书,我们将使用 Webroot 插件,该插件通过创建一个${webroot-path}/.well-known/acme-challenge临时文件来验证目录中请求的域。Lets Encrypt 服务器向临时文件发出 HTTP 请求,以验证请求的域是否解析为运行 certbot 的服务器。为了简单起见,我们将把.well-known/acme-challenge的所有HTTP请求映射到/var/lib/letsencrypt目录。

创建目录并使其可用于 Nginx 服务器。

  1. > mkdir -p /var/lib/letsencrypt/.well-known
  2. > chgrp nginx /var/lib/letsencrypt
  3. > chmod g+s /var/lib/letsencrypt

为了避免重复代码,创建以下两个片段,我们将把它们包含在我们所有的 Nginx 服务器块文件中:

  1. > mkdir /etc/nginx/snippets
  1. > cat /etc/nginx/snippets/letsencrypt.conf
  2. location ^~ /.well-known/acme-challenge/ {
  3. allow all;
  4. root /var/lib/letsencrypt/;
  5. default_type "text/plain";
  6. try_files $uri =404;
  7. }
  1. > cat /etc/nginx/snippets/ssl.conf
  2. ssl_dhparam /etc/ssl/certs/dhparam.pem;
  3. ssl_session_timeout 1d;
  4. ssl_session_cache shared:SSL:50m;
  5. ssl_session_tickets off;
  6. ssl_protocols TLSv1 TLSv1 TLSv2;
  7. ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
  8. ssl_prefer_server_ciphers on;
  9. ssl_stapling on;
  10. ssl_stapling_verify on;
  11. resolver 8 4 valid=300s;
  12. resolver_timeout 30s;
  13. add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
  14. add_header X-Frame-Options SAMEORIGIN;
  15. add_header X-Content-Type-Options nosniff;

上面的代码片段包括Mozilla推荐的配置 ,启用 OCSP 装订、HTTP 严格传输安全 (HSTS) 并强制执行少数以安全为中心的 HTTP 标头。

创建letsencrypt.conf代码段后,打开域服务器块并包含如下所示的代码段:

  1. > cat /etc/nginx/conf.d/rumenz.com.conf
  2. server {
  3. listen 80;
  4. server_name rumenz.com www.rumenz.com;
  5. include snippets/letsencrypt.conf;
  6. }

重新加载 Nginx 配置

  1. > systemctl reload nginx

你现在可以使用 webroot 插件运行 Certbot 并通过发出以下命令为你的域获取 SSL 证书文件:

  1. > certbot certonly --agree-tos --email admin@rumenz.com --webroot -w /var/lib/letsencrypt/ -d rumenz.com -d www.rumenz.com

如果成功获取 SSL 证书,certbot 将打印以下消息:

  1. IMPORTANT NOTES:
  2. - Congratulations! Your certificate and chain have been saved at:
  3. /etc/letsencrypt/live/rumenz.com/fullchain.pem
  4. Your key file has been saved at:
  5. /etc/letsencrypt/live/rumenz.com/privkey.pem
  6. Your cert will expire on 2018-06- To obtain a new or tweaked
  7. version of this certificate in the future, simply run certbot
  8. again. To non-interactively renew *all* of your certificates, run
  9. "certbot renew"
  10. - If you like Certbot, please consider supporting our work by:
  11. Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
  12. Donating to EFF: https://eff.org/donate-le

现在你有了证书文件,你可以 按如下方式编辑域服务器块:

  1. > /etc/nginx/conf.d/rumenz.com.conf
  2. server {
  3. listen 80;
  4. server_name www.rumenz.com rumenz.com;
  5. include snippets/letsencrypt.conf;
  6. return 301 https://$host$request_uri;
  7. }
  8. server {
  9. listen 443 ssl http2;
  10. server_name www.rumenz.com;
  11. ssl_certificate /etc/letsencrypt/live/rumenz.com/fullchain.pem;
  12. ssl_certificate_key /etc/letsencrypt/live/rumenz.com/privkey.pem;
  13. ssl_trusted_certificate /etc/letsencrypt/live/rumenz.com/chain.pem;
  14. include snippets/ssl.conf;
  15. include snippets/letsencrypt.conf;
  16. return 301 https://rumenz.com$request_uri;
  17. }
  18. server {
  19. listen 443 ssl http2;
  20. server_name rumenz.com;
  21. ssl_certificate /etc/letsencrypt/live/rumenz.com/fullchain.pem;
  22. ssl_certificate_key /etc/letsencrypt/live/rumenz.com/privkey.pem;
  23. ssl_trusted_certificate /etc/letsencrypt/live/rumenz.com/chain.pem;
  24. include snippets/ssl.conf;
  25. include snippets/letsencrypt.conf;
  26. # . . . other code
  27. }

通过上面的配置,我们强制使用 HTTPS 并将 www 重定向到非www 。

重启 Nginx 服务

  1. > systemctl reload nginx

打开https://rumenz.com

开启了https的网站,浏览器默认前面会加一把小锁.

图片alt

自动更新 Lets Encrypt SSL 证书

Lets Encrypt 的证书有效期为 90 天。为了在证书到期前自动更新证书,我们将创建 一个每天运行两次的cronjob,并在证书到期前 30 天自动更新任何证书。

编辑crontab定时任务

> crontab -e

  1. 0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"

要测试续订过程,你可以使用 certbot 命令后跟--dry-run开关:

  1. > certbot renew --dry-run
返回笔记列表
入门小站