HAproxy 1.5 中使用 SSL 证书
在 HAproxy 1.5 中使用 SSL 证书
返回原文英文原文:Using SSL Certificates with HAProxy
OverviewIf 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 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.
| 译者信息 有两种策略的组合做法,就是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.
| 译者信息 使用 HAProxy 作为 SSL 终端 |
First, we'll create a self-signed certificate for
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 作为示范,并在本地使用同一份证书,因为本地测试时我们服务器的IP地址可能会有所不同。例如,我们本地服务器IP是192.168.33.10,但之后我们虚拟机的IP会变成192.168.33.11, 这样我就不用重要创建自签名证书了。
虽然这个操作过程需要一份合格的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 $ 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
In any case, once we have a | 译者信息 接着,在创建了证书之后,我们需要创建 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
无论如何,只要我们得到了 HAProxy 使用的 pem 文件,我们只需经过简单配置就是可以处理SSL连接了。 |
We'll setup our application to accept both 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 OnlyIf you'd like the site to be SSL-only, you can add a 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 HAProxy with SSL Pass-ThroughWith 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 ( | 译者信息 在这个配置中,我们需要在前端和后台配置中同时使用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的区别。 |
目录 返回
首页