3、使用HAProxy作为SSL终端
首先,我们将介绍最典型的解决方案 - SSL 终端。正如前面提到的,我们需要让负载均衡器处理SSL连接。这就意味着要将SSL证书放在负载均衡服务器上。
记住,在生产环境里使用(而不是自签名)的SSL证书,是不会需要你自己来生成或签名 - 你只需要创建证书签名请求 (csr) 并把它交给那个你向它购买证书的机构即可。
首先, 我们创建一份自签名的证书作为示范,并在本地使用同一份证书。
openssl genrsa -out /etc/haproxy/wzlinux.key 2048
openssl req -new -key /etc/haproxy/wzlinux.key -out /etc/haproxy/wzlinux.csr
> Country Name (2 letter code) [AU]:CN
> State or Province Name (full name) [Some-State]:Shanghai
> Locality Name (eg, city) []:Shanghai
> Organization Name (eg, company) [Internet Widgits Pty Ltd]:wzlinux
> Organizational Unit Name (eg, section) []:
> Common Name (e.g. server FQDN or YOUR name) []:www.wzlinux.com
> Email Address []:
> Please enter the following 'extra' attributes to be sent with your certificate request
> A challenge password []:
> An optional company name []:
cd /etc/haproxy
openssl x509 -req -days 3655 -in wzlinux.csr -signkey wzlinux.key -out wzlinux.crt 这就生成了wzlinux.csr,wzlinux.key和wzlinux.crt文件了。
接着,在创建了证书之后,我们需要创建pem文件。pem文件本质上只是将证书、密钥及证书认证中心证书(可有可无)拼接成一个文件。在我们的例子中,我们只是简单地将证书及密钥文件并以这个顺序拼接在一样来创建wzlinux.pem 文件。这是HAProxy读取SSL证书首选的方式。
cat wzlinux.crt wzlinux.key | tee wzlinux.pem 当购买真正的证书 时,你不一定会获取拼接后的文件。你可以要自己拼接它们。然而,很多机构也会提供一份拼接好的文件给你。如果你没有获取到拼接后的文件,则它可能不是一个 pem 文件,而是 bundle、cert、cert、key文件或一些相同概念但名称类似的文件。
无论如何,只要我们得到了HAProxy使用的pem文件,我们只需经过简单配置就是可以处理SSL连接了。
下面我们将要配置haproxy来安装SSL证书,配置文件如下
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2 warning
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 400000
user haproxy
group haproxy
daemon
tune.ssl.default-dh-param 2048
# nbproc 3
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
option httpclose
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
stats enable
stats hide-version
stats uri /haproxy?status
stats realm Haproxy\ Statistics
stats auth admin:asd870719
# stats admin if TRUE
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend app
frontend wzlinux_ssl
bind *:80
bind *:443 ssl crt /etc/haproxy/wzlinux.pem
mode http
default_backend wzlinux
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
backend wzlinux
mode http
balance roundrobin
option forwardfor
# option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server wzlinux01 10.0.0.9:8080 check inter 15000 rise 2 fall 4
server wzlinux02 10.0.0.9:8081 check inter 15000 rise 2 fall 4
server wzlinux03 10.0.0.9:8082 check inter 15000 rise 2 fall 4
server wzlinux04 10.0.0.9:8083 check inter 15000 rise 2 fall 4
server wzlinux05 10.0.0.9:8084 check inter 15000 rise 2 fall 4
server wzlinux06 10.0.0.9:8085 check inter 15000 rise 2 fall 4
server wzlinux07 10.0.0.9:8086 check inter 15000 rise 2 fall 4
# http-request set-header X-Forwarded-Port %[dst_port]
# http-request add-header X-Forwarded-Proto https if { ssl_fc } 因为 SSL 连接在负载均衡器上终止了,我们依然来发送正常的 HTTP 请求到后台服务器。 只接受SSL连接
如果你想让网站只接受SSL连接,你可以添加向前端配置加上redirect导向: