记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

第一:我们在说什么?

注意: 这是文章的英文版本,法文版见这里.

Use Haproxy with Let's Encrypt - [EN] · Blog de Victor Héry (victor-hery.com)

haproxy :

哈代理是一个代理软件。它有很多用途,但在这里我们将使用它的能力来反向代理 HTTP 和 HTTPS。

在这篇文章中,我们将考虑您有一个有效的Haproxy服务器和一个有效的配置。

Let's Encrypt

让我们加密是由Mozilla基金会和思科赞助的开源项目。

想法是创建一个免费的证书颁发机构,以便创建SSL证书并这样做,允许大多数人使用HTTPS和安全网站。

关于SSL证书的通常成本,这是一个非常有趣的项目!

除此之外,Let's Encrypt非常易于使用,这主要归功于允许非常轻松地创建证书的API。

我们将在这篇文章中通过Let's Encrypt官方python客户端使用此API。

编辑 : 04/04/16 : 添加有关在 HTTP haproxy 前端中使用重定向 HTTP 到 HTTPS 时的情况的说明

编辑 : 09/16/16 : 使用新的二进制文件进行更新 + 添加带有一些 scritps 的 git 存储库以自动化 LE 管理

概念

Let's Encrypt提供了许多选项来通过其客户端创建和验证证书。

如果您的 Haproxy 与 Web 服务器本地化在同一台服务器上,您可以使用 --webroot 选项,该选项允许 Let's Encrypt 直接在您网站的根目录中存储一个特殊文件,以便允许 Let's Encrypt 服务器请求该文件并验证您是域的真正所有者。

如果要使用此选项,可以阅读此内容博客文章关于使用 Webroot 和 Haproxy,包括使用专用的 Apache Web 服务器进行验证。

在这篇文章中,我们将使用 --standalone 选项,它启动了一个迷你网络服务器。此网络服务器将用于验证过程,因为Let's Encrypt服务器将直接请求它。通常,此选项有一个很大的缺点:由于Web服务器绑定在80网络端口上,因此在验证过程中需要暂时关闭真正的Web服务器。

换句话说,您的网站将在整个过程中不可用。

但是我们将使用一个选项来更改默认端口,与haproxy前端/后端功能协调,以允许在没有任何停机时间的情况下进行验证:-)

-- 非常感谢酷拉86因为他的帖子给了我这个想法,一起使用haproxy和Let's Encrypt ;-) -

获取让我们加密客户端

随着LE现在正式发布,一个新的官方二进制文件可用,比以前的letsencrypt-auto更先进,更易于使用。

所有信息均可在认证机器人上找到官方网站,但这里有如何使用它的快速说明。

cd /root/

mkdir letsencrypt

cd letsencrypt

wget https://dl.eff.org/certbot-auto

chmod a+x certbot-auto


接下来,您可以直接使用二进制文件来获取新证书(至少,一旦您按照本博客文章的描述进行了配置:-p)

./certbot-auto certonly  --domains blog.victor-hery.com --renew-by-default --http-01-port 63443 --agree-tos

配置 haproxy

前端

为避免任何停机时间,我们将使用您现有的前端。

Let's Encrypt将请求解析您网站的IP地址,因此,如果您有许多前端或许多IP,则需要根据需要配置每个前端。

主要思想是:在此过程中,Let's Encrypt请求基本网站URL,然后是/.well-known/acme-challenge/a_unique_id。

So we are going to configure a haproxy ACL which match this path to redirect it to a specific backend!

frontend http-in
    acl app_letsencrypt  path_beg   /.well-known/acme-challenge/
    [...]
    use_backend bk-letsencrypt if app_letsencrypt
  • path_beg: match the path (the part just after the first /) that begin by .well-known/acme-challenge/

Doing so, all Let's Encrypt requests will be redirected to the bk-letsencrypt backend.

Warning : if you are using redirect from HTTP to HTTPS for your website, haproxy will also redirect Let's Encrypt request to your HTTPS frontend.

You will then need to add the acl and use_backend lines to your HTTPS frontend as well, or let's encrypt will get 404 not found answer.

Backend

About the backend, we are going to configure it to redirect request to the server launched by Let's Encrypt client.

backend bk-letsencrypt
    log global
    mode http
    server srv_letsencrypt 127.0.0.1:63443
  • 模式 http:允许检查请求的 HTTP 一致性
  • 服务器:此行重定向到 Let's Encrypt 客户端将在本地主机上启动的服务器,端口 63443

本地服务器不会始终处于启动状态,仅在客户端运行时启动。其余时间,如果有人尝试获取与ACL匹配的URL,haproxy 将返回 503 错误。

当然,您需要在进行这些修改后重新加载 haproxy。

systemctl reload haproxy.service```

## Configure and use of Let's Encrypt

#### Configuration :

<p>To simplify the command line usage, we use a configuration file for Let's Encrypt client.</p>

<p>By default, the client uses the file <em>/etc/letsencrypt/cli.ini</em>. So this is the file we are going to edit.</p>

RSA 密钥大小 = 4096 电子邮件 = your_admin_email 身份验证器 = 独立 独立支持的挑战 = HTTP-01

<ul>
    <li>rsa-key-size: tell letsencrypt to direclty generate 4096 bits certificate, more strong that default 2048. You can downgrade to 2048 (but never less!) if your server is low performance to gain some generation time</li>
    <li>email: use a valid email address, as it will be used if certificate recovery is needed through the Let's Encrypt website.</li>
    <li>authenticator: as seen before, we will use <em>standalone</em> mode</li>
    <li>standalone-supported-challenges: this option is specific to standalone mode, and allow to choose the method used for the verification process, between <em>http-01</em> and <em>tls-sni-01</em>. Here we use http-01 as our website has no valid certificate (the first time), and so haproxy will not have valid SSL certificate to use in its frontend for Let's Encrypt server request.</li>
</ul>

<p>Using HTTP for the verification process is safe, as only the verification request will be send in clear, and it has no secret inside.</p>

### Generate the certificate

<p>The command line to use to generate your certificate is the following:</p>

/root/letsencrypt/certbot-auto certonly --domains yourdomain.tld --renew-by-default --http-01-port 63443 --agree-tos'''

  • Certonly:告诉客户端我们只想生成证书,而不是使用某些安装插件在某处安装证书。
  • 域:要为其颁发证书的域或子域
  • 默认续订:告诉LetsEncrypt,如果证书已经存在,它应该续订它。如果它不存在,它将创建它。
  • HTTP-01-port:告知网络端口用于启动的临时验证服务器。这是我们之前配置的 haproxy 后端中使用的端口。
  • 同意:告诉客户自动接受Let's Encrpt的服务时间(如果你在这里,你已经阅读并同意了,对吧?

安装证书

证书由客户端在生成过程中在名为 /etc/letsencrypt/live/yourdomain.tld/ 的目录中创建。

您会在里面找到多个文件:

  • Cert.PEM : 证书公共部分 (CRT)
  • chain.pem : 权威链 (CA of Authority)
  • fullchain.pem :cert.pem 和 chain.pem 的串联
  • privkey.pem : 认证私钥

为了与haproxy一起使用,您需要连接fullchain.pem和privkey.pem,并将其存储在haproxy读取其证书的位置。

cat fullchain.pem privkey.pem &gt; domain.tld.pem```

<p>For example for the following HTTPS frontend:</p>

前端 https-in 绑定 IP:443 ssl crt /etc/haproxy/cert/

<p>Here we need to store the domain.tld.pem file in <em>/etc/haproxy/cert/</em></p>

<p>Once your certificate is stored in the right place, reload haproxy for it to re-read certificate, and everything should be OK.</p>

<p>Of course do not forget to configure an HTTPS frontend with correct ACL for your website in haproxy!</p>

<p>Usually, simply copy/paste of your HTTP frontend changing port to 443 (and adding ssl and crt option) will do the job.</p>

## Limitations

### Renew the certificate

<p>Even from the end of beta time, LE has taken decision to provide certificate for 90 days duration max. It allow them to avoid abuse usage and to make the project alive by forcing regular renew.</p>

<p>Note that unlilke the beta client, certbot-auto is capable to use all your available certificates (thanks to the directory<em>/etc/letsencrypt/renewal/</em>) to allow automatic renewal .</p>

<p>So remember to configure a cron job on your server to renew certificate:</p>

crontab -e


续订证书

30 01 01,10,20,30 * * /root/letsencrypt/certbot-auto renew'''

此 cron 任务将每 10 天启动一次续订命令,以确保您的证书保持有效。

不要忘记,您仍然需要从fullchain.pem创建.pem,并.key将其提供给haproxy。有关此任务的一些自动化,请参见下文!

按域划分的证书速率限制

由于其免费使用,Let's Encrypt使用速率限制系统来避免为同一域生成过多的证书。

如果您尝试为同一域生成太快或太频繁的证书,则会收到错误

请注意:限制也考虑了子域!所有以 domain.tld 结尾的证书都将被计算在内。

到:

目前,Let's Encrypt不允许国际域名的证书。

这不可能为包含重音符号或特殊字符的域或子域生成证书。

例如,不幸的是,无法为 https://blog.héry.com 生成证书。

目前有一个漂砾在Let's Encrypt github上,但它仍然不可用。如果您愿意帮助或需要该功能,请随时加入:-)

奖励:一些脚本

为了使过程更容易,我编写了一些脚本来生成和更新证书,以及haproxy交互,更容易。

它们在我的Git 存储库,因此您可以在服务器中轻松克隆它们:

git clone https://git.lecygnenoir.info/LecygneNoir/letsencrypt-haproxy.git

自述文件只是简单地描述了如何使用它们,它非常简单。

create-certificate允许您为传递给脚本的域创建证书,然后为haproxy创建.pem,将其存储在给定的目录中并重新加载haproxy。

续订证书仅续订所有需要续订的证书,还创建 haproxy PEM 文件,en 重新加载 HAPROXY。如果需要,您可以在我们的 cron 任务中使用续订证书,如前所述。

不要忘记检查脚本中的路径,主要是存储haproxy证书的位置,以及certbot二进制文件的路径

瞧,有了所有这些,您应该能够创建所需的所有证书,并直接在 haproxy 中使用它们,而无需停机!所以,欢迎来到HTTPS世界:-)



转载请标明出处【将 Haproxy 与 Let's Encrypt 一起使用】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论