Nginx 使用 Let's Encrypt 证书实现 HTTPS 访问

2016-09-17 20:31
  1. 安装Let's Encrypt客户端
  2. 获得Let's Encrypt证书
  3. 生成Diffie-Hellman Group(可选)
  4. 修改nginx配置
  5. 设置自动更新证书

安装Let's Encrypt客户端

sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt

获得Let's Encrypt证书

必须要先关闭占据80端口的程序

sudo ./letsencrypt-auto certonly --standalone

输入需要生成证书的域名:

Please enter in your domain name(s) (comma and/or space separated)
------------------------------------------------------------------
|                                                                |
------------------------------------------------------------------
           <OK>                            <Cancel>

结果如下:

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/hooog.tk/fullchain.pem. Your cert will expire
on 2016-12-03. To obtain a new or tweaked version of this
certificate in the future, simply run letsencrypt-auto again. To
non-interactively renew *all* of your certificates, run
"letsencrypt-auto 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

Your cert will expire on 2016-12-03 注意过期时间

生成Diffie-Hellman Group(可选)

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

修改nginx配置

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    { your config... }
}

启用更安全的SSL protocols及ciphers

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;

重定向非HTTPS请求

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

设置自动更新证书

sudo /opt/letsencrypt/letsencrypt-auto renew

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/hooog.tk.conf
-------------------------------------------------------------------------------

The following certs are not due for renewal yet:
/etc/letsencrypt/live/hooog.tk/fullchain.pem (skipped)
No renewals were attempted.

由于证书是最近更新过的, 所以 renew 命令只检查过期时间

设置自动更新,每天更新一次并重启Nginx

sudo crontab -e

0 4 * * 1 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log
5 4 * * 1 /etc/init.d/nginx reload