背景
如上述话题说言 https需要映射多个server 而我们多个server使用了负载均衡进行映射到不同的服务器上
certbot的原理 CertBot在验证服务器域名的时候,会生成一个随机文件,然后CertBot的服务器会通过HTTP访问你的这个文件
因此如果使用nginx做负载均衡时可能就会出现长时间404
因此建议在nginx加上server配置
原理
certbot会生成某个文件一般形如
http://agent.f6car.com/.well-known/acme-challenge/7kpkF-IKm00Y2tfdo_d6k8mA0vuJbITnyA35C0LLh2U
通常都是新建文件夹为.well-known/acme-challenge后面加上随机文件名称即可
CertBot的服务器会通过HTTP访问你的这个文件 如果访问成功则认为该域名为当前用户所拥有
因此我们可以如下操作
在nginx对应配置上增加
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/local/nginx/html;
}
location = /.well-known/acme-challenge/ {
return 404;
}
那么每个域名在访问/.well-known/acme-challenge/都会映射到/usr/local/nginx/html
操作
我们执行certbot指令 比如
./certbot-auto certonly --email [email protected] --agree-tos --webroot -w /usr/local/nginx/html -d shop.f6car.com
这样cert会在/usr/local/nginx/html生成刚刚所描述的文件 同时远程服务器成功访问通过
如此将会生成对应的证书
# ls /etc/letsencrypt/live
agent.f6car.com if.f6car.com if-trial.f6car.com m.f6car.com m-trial.f6car.com report.f6car.com report-trial.f6car.com shop.f6car.com www.f6car.com yunxiu.f6car.com yunxiu-trial.f6car.com