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

在 HAproxy 1.5 中使用 SSL 证书

返回原文英文原文:Using SSL Certificates with HAProxy

Overview

If your application makes use of SSL certificates, then some decisions need to be made about how to use them with a load balancer.

A simple setup of one server usually sees a client's SSL connection being decrypted by the server receiving the request. Because a load balancer sits between a client and one or more servers, where the SSL connection is decrypted becomes a concern.

译者信息

概述

如果你的应用使用SSL证书,则需要决定如何在负载均衡器上使用它们。

服务器的简单配置通常是考虑客户端SSL连接如何被接收请求的服务器解码。由于负载均衡器处在客户端和更多服务器之间,SSL连接解码就成了需要关注的焦点。

There are two main strategies.

SSL Termination is the practice of terminating/decrypting an SSL connection at the load balancer, and sending unencrypted connections to the backend servers.

This means the load balancer is responsible for decrypting an SSL connection - a slow and CPU intensive process relative to accepting non-SSL requests.

This is the opposite of SSL Pass-Through, which sends SSL connections directly to the proxied servers.

译者信息

有两种主要的策略。

SSL终端是在负载均衡器终止/解码SSL连接并发送非加密连接到后台服务器的做法

这意味着负载均衡器负责解码SSL连接 - 涉及非SSL请求的缓慢的CPU密集型处理。

这与SSL穿透相反, 它是直接向代理服务器发送SSL连接的。

With SSL-Pass-Through, the SSL connection is terminated at each proxied server, distributing the CPU load across those servers. However, you lose the ability to add or edit HTTP headers, as the connection is simply routed through the load balancer to the proxied servers.

This means your application servers will lose the ability to get the X-Forwarded-* headers, which may include the client's IP address, port and scheme used.

Which strategy you choose is up to you and your application needs. SSL Termination is the most typical I've seen, but pass-thru is likely more secure.

译者信息

使用SSL穿透,SSL连接在每个代理服务器终止,将CPU负载都分散到那些服务器。然而,这样做会让你失去增加或修改HTTP报头的能力,因为连接只是简单地从负载均衡器路由到代理服务器。

这意味着应用服务器会失去获取 X-Forwarded-* 报头的能力,这个报头包含了客户端IP地址、端口和使用的协议。

选择哪个策略取决于你及应用的需求。SSL终端为我所见过最典型的策略,但SSL穿透可能会更安全。

There is a combination of the two strategies, where SSL connections are terminated at the load balancer, adjusted as needed, and then proxied off to the backend servers as a new SSL connection. This may provide the best of both security and ability to send the client's information. The trade off is more CPU power being used all-around, and a little more complexity in configuration.

An older article of mine on the consequences and gotchas of using load balancers explains these issues (and more) as well.

译者信息

有两种策略的组合做法,就是SSL连接在负载均衡器处终止,按需求调整,然后作为新的SSL连接代理到后台服务器。这可能会提供最大的安全性和发送客户端信息的能力。这样做的代价是更多的CPU能耗和稍复杂一点的配置。

我的一篇关于负载均衡器的效果与性能及哥伸缩性的老文章也有讨论过这些问题。

HAProxy with SSL Termination

We'll cover the most typical use case first - SSL Termination. As stated, we need to have the load balancer handle the SSL connection. This means having the SSL Certificate live on the load balancer server.

We saw how to create a self-signed certificate in a previous edition of SFH. We'll re-use that information for setting up a self-signed SSL certificate for HAProxy to use.

Keep in mind that for a production SSL Certificate (not a self-signed one), you won't need to generate or sign a certificate yourself - you'll just need to create a Certificate Signing Request (csr) and pass that to whomever you purchase a certificate from.

译者信息

使用 HAProxy 作为 SSL 终端

首先,我们将介绍最典型的解决方案 - SSL 终端。正如前面提到的,我们需要让负载均衡器处理SSL连接。这就意味着要将SSL证书放在负载均衡服务器上。

在之前的SFH中,我们已经介绍过如何创建自签名证书。我们将重用那些信息来创建一份给HAProxy使用的SSL证书。

记住,在生产环境里使用(而不是自签名)的SSL证书,是不会需要你自己来生成或签名 - 你只需要创建证书签名请求 (csr) 并把它交给那个你向它购买证书的机构即可。

First, we'll create a self-signed certificate for *.xip.io, which is handy for demonstration purposes, and lets use one the same certificate when our server IP addresses might change while testing locally. For example, if our local server exists at 192.168.33.10, but then our Virtual Machine IP changes to 192.168.33.11, then we don't need to re-create the self-signed certificate.

I use the xip.io service as it allows us to use a hostname rather than directly accessing the servers via an IP address, all without having to edit my computers' Host file.

As this process is outlined in a passed edition on SSL certificates, I'll simple show the steps to generate a self-signed certificate here:

$ sudo mkdir /etc/ssl/xip.io
$ sudo openssl genrsa -out /etc/ssl/xip.io/xip.io.key 1024$ sudo openssl req -new -key /etc/ssl/xip.io/xip.io.key -out /etc/ssl/xip.io/xip.io.csr> Country Name (2 letter code) [AU]:US> State or Province Name (full name) [Some-State]:Connecticut> Locality Name (eg, city) []:New Haven> Organization Name (eg, company) [Internet Widgits Pty Ltd]:SFH> Organizational Unit Name (eg, section) []:> Common Name (e.g. server FQDN or YOUR name) []:*.xip.io> Email Address []:> Please enter the following 'extra' attributes to be sent with your certificate request> A challenge password []:> An optional company name []:$ sudo openssl x509 -req -days 365 -in /etc/ssl/xip.io/xip.io.csr -signkey /etc/ssl/xip.io/xip.io.key -out /etc/ssl/xip.io/xip.io.crt

This leaves us with a xip.io.csr, xip.io.key and xip.io.crt file.

译者信息

首先, 我们创建一份自签名的证书给 *.xip.io 作为示范,并在本地使用同一份证书,因为本地测试时我们服务器的IP地址可能会有所不同。例如,我们本地服务器IP是192.168.33.10,但之后我们虚拟机的IP会变成192.168.33.11, 这样我就不用重要创建自签名证书了。

我使用 xip.io 服务是因为它允许我们使用主机名而非直接通过IP访问服务器,这就不需要悠我电脑的Host文件了

虽然这个操作过程需要一份合格的SSL证书, 但我会在这里简单地演示生成自签名证书的步骤:

$ sudo mkdir /etc/ssl/xip.io
$ sudo openssl genrsa -out /etc/ssl/xip.io/xip.io.key 1024
$ sudo openssl req -new -key /etc/ssl/xip.io/xip.io.key -out /etc/ssl/xip.io/xip.io.csr
> Country Name (2 letter code) [AU]:US
> State or Province Name (full name) [Some-State]:Connecticut
> Locality Name (eg, city) []:New Haven
> Organization Name (eg, company) [Internet Widgits Pty Ltd]:SFH
> Organizational Unit Name (eg, section) []:
> Common Name (e.g. server FQDN or YOUR name) []:*.xip.io
> Email Address []:
> Please enter the following 'extra' attributes to be sent with your certificate request
> A challenge password []:
> An optional company name []:
$ sudo openssl x509 -req -days 365 -in /etc/ssl/xip.io/xip.io.csr -signkey /etc/ssl/xip.io/xip.io.key -out /etc/ssl/xip.io/xip.io.crt

这就生成了axip.io.csr,xip.io.key和xip.io.crt文件了。

Next, after the certificates are created, we need to create a pem file. A pem file is essentially just the certificate, the key and optionally certificate authorities concatenated into one file. In our example, we'll simply concatenate the certificate and key files together (in that order) to create a xip.io.pem file. This is HAProxy's preferred way to read an SSL certificate.

$ sudo cat /etc/ssl/xip.io/xip.io.crt /etc/ssl/xip.io/xip.io.key | sudo tee /etc/ssl/xip.io/xip.io.pem

When purchasing a real certificate, you won't necessarily get a concatenated "bundle" file. You may have to concatenate them yourself. However, many do provide a bundle file. If you do, it might not be a pem file, but instead be a bundle, cert, cert, key file or some similar name for the same concept. This Stack Overflow answer explains that nicely.

In any case, once we have a pem file for HAproxy to use, we can adjust our configuration just a bit to handle SSL connections.

译者信息

接着,在创建了证书之后,我们需要创建 pem 文件。pem 文件本质上只是将证书、密钥及证书认证中心证书(可有可无)拼接成一个文件。在我们的例子中,我们只是简单地将证书及密钥文件并以这个顺序拼接在一样来创建 xip.io.pem 文件。这是 HAProxy 读取SSL证书首选的方式。

$ sudo cat /etc/ssl/xip.io/xip.io.crt /etc/ssl/xip.io/xip.io.key | sudo tee /etc/ssl/xip.io/xip.io.pem

当购买真正的证书 时,你不一定会获取拼接后的文件。你可以要自己拼接它们。然而,很多机构也会提供一份拼接好的文件给你。如果你没有获取到拼接后的文件,则它可能不是一个 pem 文件,而是 bundle、cert、cert、key文件或一些相同概念但名称类似的文件。这个 Stack Overflow 答案对问题解释得很好。

无论如何,只要我们得到了 HAProxy 使用的 pem 文件,我们只需经过简单配置就是可以处理SSL连接了。

We'll setup our application to accept both http and https connections. In the last edition on HAProxy, we had this frontend:

frontend localnodes
    bind *:80
    mode http
    default_backend nodes

To terminate an SSL connection in HAProxy, we can now add a binding to the standard SSL port 443, and let HAProxy know where the SSL certificates are:

frontend localhost
    bind *:80
    bind *:443 ssl crt /etc/ssl/xip.io/xip.io.pem
    mode http
    default_backend nodes

In the above example, we're using the backend "nodes". The backend, luckily, doesn't really need to be configured in any particular way. In the previous edition on HAProxy, we had the backend like so:

backend nodes
    mode http
    balance roundrobin
    option forwardfor
    option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    server web01 172.17.0.3:9000 check
    server web02 172.17.0.3:9001 check
    server web03 172.17.0.3:9002 check
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }

Because the SSL connection is terminated at the Load Balancer, we're still sending regular HTTP requests to the backend servers. We don't need to change this configuration, as it works the same!

译者信息

我们将配置我们的应用以同时接受 http 和 https 连接。在最新版的 HAProxy中,我们配置这样的前端:

frontend localnodes
    bind *:80
    mode http
    default_backend nodes

要在 HAProxy 里终止 SSL 连接,我们现在就可以添加校准 SSL端 口 443 的绑定,并让 HAProxy 知道 SSL 证书的位置:

frontend localhost
    bind *:80
    bind *:443 ssl crt /etc/ssl/xip.io/xip.io.pem
    mode http
    default_backend nodes

在上面的例子中,我们使用了后台"nodes"。幸运的是,后台不需要特别配置。在之前的 HAProxy 版本中,我们要这样配置后台:

backend nodes
    mode http
    balance roundrobin
    option forwardfor
    option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    server web01 172.17.0.3:9000 check
    server web02 172.17.0.3:9001 check
    server web03 172.17.0.3:9002 check
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }

因为 SSL 连接在负载均衡器上终止了,我们依然来发送正常的 HTTP 请求到后台服务器。我们不需要更改配置就可以得到同样的效果了!

SSL Only

If you'd like the site to be SSL-only, you can add a redirect directive to the frontend configuration:

frontend localhost
    bind *:80
    bind *:443 ssl crt /etc/ssl/xip.io/xip.io.pem
    redirect scheme https if !{ ssl_fc }
    mode http
    default_backend nodes

Above, we added the redirect directive, which will redirect from "http" to "https" if the connection was not made with an SSL connection. More information on ssl_fc is available here.

HAProxy with SSL Pass-Through

With SSL Pass-Through, we'll have our backend servers handle the SSL connection, rather than the load balancer.

The job of the load balancer then is simply to proxy a request off to its configured backend servers. Because the connection remains encrypted, HAProxy can't do anything with it other than redirect a request to another server.

译者信息

只接受SSL连接

如果你想让网站只接受SSL连接,你可以添加向前端配置加上 redirect 导向:

frontend localhost
    bind *:80
    bind *:443 ssl crt /etc/ssl/xip.io/xip.io.pem
    redirect scheme https if !{ ssl_fc }
    mode http
    default_backend nodes

上面,我们添加了 redirect 导向,如果连接不是通过SSL连接的,它将 http 重定向到 https。更多信息参见 ssl_fcis available here

使用HAProxy实现SSL穿透

使用SSL穿透,我们将让后台服务器处理SSL连接,而非负载均衡器来处理。

负载均衡器的工作就只是简单地将请求转发到配置好的后台服务器。因为连接还保持加密状态,HAProxy只能将它转发给其他服务器,其他事情就没法做了。

In this setup, we need to use TCP mode over HTTP mode in both the frontend and backend configurations. HAProxy will treat the connection as just a stream of information to proxy to a server, rather than use its functions available for HTTP requests.

First, we'll tweak the frontend configuration:

frontend localhost
    bind *:80
    bind *:443
    option tcplog
    mode tcp
    default_backend nodes

This still binds to both port 80 and port 443, giving the opportunity to use both regular and SSL connections.

As mentioned, to pass a secure connection off to a backend server without encrypting it, we need to use TCP mode (mode tcp) instead. This also means we need to set the logging to tcp instead of the default http (option tcplog). Read more on log formats here to see the difference between tcplog and httplog.

译者信息

在这个配置中,我们需要在前端和后台配置中同时使用TCP模式而不是HTTP模式。HAProxy只会把连接当作信息流来转发到其他服务器,而不会使用在HTTP请求上才能使用的功能。

首先,我们调整一下前端配置:

frontend localhost
    bind *:80
    bind *:443
    option tcplog
    mode tcp
    default_backend nodes

这里依然同时绑定80和443端口,以保证正常的HTTP连接和SSL连接都能工作。

正如上述提到的,转发一个安全连接事台服务器而不作任何解码,我们需要使用TCP模式(mode tcp)。这也意味着我们需要设置tcp日志而不是默认的http日志(option tcplog)。阅读这个可以了解日志格式并搞清楚tcplog和httplog的区别。



转载请标明出处【HAproxy 1.5 中使用 SSL 证书】。

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

网站已经关闭评论