Docker 自动更新 Let’s Encrypt

在nginx的 docker run 中添加webroot和配置文件挂载

-v $PWD/nginx/letsencrypt/:/var/www/letsencrypt:ro \
-v $PWD/letsencrypt/etc/:/etc/nginx/letsencrypt/:ro \

在nginx中将wwwroot发布出去

location ^~ /.well-known/ {
    root /var/www/letsencrypt/;
}

在nginx中配置证书文件

ssl_certificate letsencrypt/live/www.yaoge123.com/fullchain.pem;
ssl_certificate_key letsencrypt/live/www.yaoge123.com/privkey.pem;

创建 certbot 的docker run脚本,以后只要周期性运行这个脚本就可以自动更新证书了

#!/bin/sh
cd $(dirname $0)
pwd

docker run -it --rm \
	-v $PWD/letsencrypt/etc:/etc/letsencrypt \
	-v $PWD/letsencrypt/lib:/var/lib/letsencrypt \
	-v $PWD/letsencrypt/log:/var/log/letsencrypt \
	-v $PWD/nginx/letsencrypt:/var/www \
	certbot/certbot \
	certonly --webroot \
	--email yaoge123@example.com --agree-tos --no-eff-email \
	--webroot-path=/var/www/ \
	-n \
	--domains www.yaoge123.com
docker kill --signal=HUP nginx

Nginx HTTPS配置

配置很简单,但是需要注意的有:

  1. 应使用listen的ssl参数取代ssl on;
  2. cert文件应包含整个证书链,内容顺序必须是证书链的逆序,即cert文件头是本服务器的证书、中间是中间CA、最后是根CA。可以用下述命令检查
    openssl s_client -connect www.example.com:443
  3. ssl_protocols中只能包含TLS
    server {
            listen          [::]:80;
            listen          [::]:443 ssl;
    ……
            ssl_certificate /etc/ssl/microstructures_bundle.crt;
            ssl_certificate_key /etc/ssl/microstructures_org.key;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers on;
    ……
    }