由于这是一项非常常见的任务,本文将指导您逐步完成使用 HTTPS 保护您的网站(和您的用户)的过程。这里的特定部分是我们将在 docker 环境中执行此操作。
在这篇文章中,我将使用 Docker Compose 来简化教程。
1. Nginx 作为服务器
为了能够将 nginx 用作我们任何项目的服务器,我们必须为其创建一个 Docker Compose 服务
version: '3'
services:
webserver:
image: nginx:latest
ports:
- 80:80
- 443:443
restart: always
volumes:
- ./nginx/conf/:/etc/nginx/conf.d/:ro
- ./certbot/www:/var/www/certbot/:ro
您都可以运行docker compose up以启动环境并查看是否一切顺利。
- 它侦听
80HTTP 和443HTTPS 的端口。 - 因为我希望服务器始终启动并运行,所以我告诉 Docker 它应该负责在意外关闭时重新启动“webserver”服务。
- 我们安装的最终目标不是为 nginx 的默认欢迎页面提供服务。因此,我们需要一种方法来更新 nginx 配置并声明我们的网站。为此,我们使用了 Docker 的“卷”功能。
/etc/nginx/conf.d/这意味着我们将位于 docker 容器的文件夹映射到位于./nginx/conf/我们本地计算机上的文件夹。我们在本地添加、删除或更新到此文件夹中的每个文件都将更新到容器中。 - 请注意,我
:ro在卷声明的末尾添加了一个。ro意思是“只读”。容器将永远无权将文件更新到此文件夹中。但它是最佳实践。它可以避免浪费几个宝贵的调试时间。 - 将以下配置文件添加到您的
./nginx/conf/本地文件夹中。不要忘记使用您自己的数据进行更新。
server {
listen 80;
listen [::]:80;
server_name example.org www.example.org;
server_tokens off;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://example.org$request_uri;
}
}
配置很简单。我们向 nginx 解释它必须监听80特定域名的端口(在 IPv4 或 IPv6 上)example.org。
默认情况下,我们希望将来自 port 的人重定向80到相同的路由,但在 port 上443。这就是我们对location /块所做的。
但这里的特殊性是另一个location块。它提供 Certbot 验证我们的服务器并为其创建 HTTPS 证书所需的文件。
基本上,我们说“除了/.well-know/acme-challenge/路由之外总是重定向到 HTTPS”。
我们现在可以通过粗略地重新加载 nginx,
docker compose restart
或者如果你想避免服务中断(即使是几秒钟)使用
docker compose exec webserver nginx -s reload.
2. 使用 Certbot 创建证书
2.1 添加 certbot服务
目前,不会显示任何内容,因为 nginx 一直将您重定向到443nginx 尚未处理的端口。但一切都很好。我们只希望 Certbot 能够验证我们的服务器。
为此,我们需要为 certbot 使用 docker 镜像,并将其作为服务添加到我们的 Docker Compose 项目中。
version: '3'
services:
webserver:
image: nginx:latest
ports:
- 80:80
- 443:443
restart: always
volumes:
- ./nginx/conf/:/etc/nginx/conf.d/:ro
- ./certbot/www:/var/www/certbot/:ro
certbot:
image: certbot/certbot:latest
volumes:
- ./certbot/www/:/var/www/certbot/:rw
我们现在有两种服务,一种用于 nginx,一种用于 Certbot。您可能已经注意到他们声明了相同的卷。这是为了让他们一起交流。
Certbot 将其文件写入./certbot/www/,nginx 将在端口上80为每个请求/.well-know/acme-challenge/. 这就是 Certbot 可以验证我们的服务器的方式。
请注意,对于 Certbot,我们:rw在卷声明的末尾使用了它代表“读写”。如果不这样做,它将无法写入文件夹并且身份验证将失败。
您现在可以通过运行来测试一切是否正常\
docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ --dry-run -d example.org
您应该会收到一条成功消息,例如: "The dry run was successful".
现在我们可以为服务器创建证书,我们想在 nginx 中使用它们来处理与最终用户浏览器的安全连接。
Certbot 在文件夹中创建证书/etc/letsencrypt/。与 webroot 的原理相同,我们将使用卷在容器之间共享文件。
version: '3'
services:
webserver:
image: nginx:latest
container_name: webserver
ports:
- 80:80
- 443:443
restart: always
volumes:
- ./nginx/conf/:/etc/nginx/conf.d/:ro
- ./certbot/www:/var/www/certbot/:ro
- ./certbot/conf/:/etc/nginx/ssl/:ro
certbot:
image: certbot/certbot:latest
container_name: cerbot
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
2.2 启动cerbot
使用 重新启动您的容器docker compose restart。Nginx 现在应该可以访问 Certbot 创建证书的文件夹。
但是,这个文件夹现在是空的。在没有标志的情况下重新运行 Certbot--dry-run以用证书填充文件夹:
$ docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ -d example.org
由于我们有这些证书,剩下的就是 nginx 443 端口监听的配置。
2.3 nginx 添加 443 监听配置
server {
listen 80;
listen [::]:80;
server_name example.org www.example.org;
server_tokens off;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://example.org$request_uri;
}
}
server {
listen 443 default_server ssl http2;
listen [::]:443 ssl http2;
server_name example.org;
ssl_certificate /etc/nginx/ssl/live/example.org/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/example.org/privkey.pem;
location / {
# ...
}
}
3. 重新加载 nginx 服务器
重新加载 nginx 服务器将使其能够处理使用 HTTPS 的安全连接。Nginx 使用来自 Certbot 卷的证书和私钥。
docker restart webserver
4. 续签
4.1 手动续签
Certbot 和 Let's Encrypt 可能会遇到的一个小问题是证书只能使用 3 个月, 所以您将需要定期更新您使用的证书。
但是由于我们已经有了这个 Docker 环境,更新 Let's Encrypt 证书比以往任何时候都容易!
docker compose run --rm certbot renew
4.2 自动续签
# 注意 cd /data/app/compose 进入你自己的应用compose 目录
# mkdir -p /var/log/certbot
# crontab -e
0 0 1 * * cd /data/app/compose; docker compose run --rm certbot renew --force-renewal >> /var/log/certbot/renew.log 2>&1; docker restart proxy_nginx >> /var/log/certbot/renew.log 2>&1
5. 泛域名【通配符域名】*.domain.com
如果是通配符域名的话,会报错:
Client with the currently selected authenticator does not support any combination of challenges that will satisfy the CA. You may need to use an authenticator plugin that can do challenges over DNS.
无法按照上述方法生成证书,因为还需要通过 dns-01 方式验证
参考如下链接:
blog.csdn.net/wc810267705…
参考链接:
mindsers.blog/post/https-…
eff-certbot.readthedocs.io/en/stable/u…
letsencrypt.osfipin.com/user-0408/o…