如何为多个 SSL 证书配置 HAProxy

mer*_*lin 9 ssl haproxy ssl-certificate

我需要使用两个不同的 SSL 证书配置 HAProxy

  1. www.example.com
  2. api.example.com

现在我从关于 serverfault 的帖子(在 Haproxy 中配置多个 SSL 证书)中了解到如何使用 2 个证书,但是服务器继续使用为两个域提到的第一个证书。

配置:

frontend apache-https
    bind 192.168.56.150:443 ssl crt /certs/crt1.pem crt /certs/cert2.pem
    reqadd X-Forwarded-Proto:\ https
    default_backend apache-http

backend apache-http
    redirect scheme https if { hdr(Host) -i www.example.com } !{ ssl_fc }
    redirect scheme https if { hdr(Host) -i api.example.com } !{ ssl_fc }
    ...
Run Code Online (Sandbox Code Playgroud)

如何根据 URL 告诉 HAProxy 使用哪个证书?

完整配置:

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

    # Default SSL material locations
    ca-base /etc/ssl/certs
    crt-base /etc/ssl/private

    ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
    ssl-default-bind-options no-sslv3
    tune.ssl.default-dh-param 2048 // better with 2048 but more processor intensive

defaults
        log     global
        mode    http
        option tcplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000

frontend apache-http
        bind 0.0.0.0:80
        mode http
        option http-server-close                # needed for forwardfor
        option forwardfor                       # forward IP Address of client
        reqadd X-Forwarded-Proto:\ http
        default_backend apache-http
        stats enable

frontend apache-https
        bind 0.0.0.0:443 ssl crt cer1.pem cert2.pem
        reqadd X-Forwarded-Proto:\ https
        default_backend apache-http

backend apache-http
        redirect scheme https if { hdr(Host) -i db.example.com } !{ ssl_fc }
        redirect scheme https if { hdr(Host) -i api2.example.com } !{ ssl_fc }
        balance roundrobin
        cookie SERVERID insert indirect nocache
        server www-1 10.0.0.101:80 cookie S1 check
        server www-2 10.0.0.102:80 cookie S2 check
        server www-3 10.0.0.103:80 cookie S3 check
Run Code Online (Sandbox Code Playgroud)

小智 10

确保您运行的是 HAProxy 1.6 或更高版本

这个问题有点旧,但我遇到了与 OP 类似的配置完全相同的问题。

HAProxy 1.5 接受多个选项的crt语法bind;但是,它在响应时仅使用第一个证书。

HAProxy 1.6 似乎根据调用者的请求使用证书进行响应。这似乎不需要sni配置中的任何特殊ACL。

这是一个适用于 1.6 的示例,但cert2.pem在响应place2.com1.5 的请求时无法使用:

frontend http-in
        bind *:80
        bind *:443 ssl crt cert1.pem crt cert2.pem
        mode http

        acl common_dst hdr(Host) -m str place1.com place2.com

        use_backend be_common if common_dst

backend be_common
        # nothing special here.
Run Code Online (Sandbox Code Playgroud)


wom*_*ble 2

您如何测试 haproxy 提供的证书?如果您使用openssl s_client,请注意它需要一个附加参数 ( -servername api.domain.com) 才能发送 SNI 信息,haproxy 需要该信息来决定提供哪个证书。

  • “但是服务器继续使用两个域提到的第一个证书”——如果不是您自己的测试,那么您问题中这一断言的基础是什么? (3认同)