我想使用HAProxy终止TLS加密的TCP连接,并根据用于启动TLS连接的服务器名称指示将未加密的TCP流量传递到各种后端。
我在后端服务器上运行3个服务,每个服务都在不同的端口(5001、5002、5003)上。 HAProxy绑定到端口5000。我想按名称将连接路由到前2个服务,如果没有匹配项,则路由到第三个。我正在使用openssl s_client
启动到HAProxy的连接。但是,在日志中,我可以看到连接仅路由到默认服务器,即SNI似乎被忽略。
DNS:
A record demo.sni.example.com 1.2.3.4
CNAME 1.sni.example.com pointing to demo.sni.example.com
CNAME 2.sni.example.com pointing to demo.sni.example.com
即我希望进行以下路由:
SNI = 1.sni.example.com:5000 -> 1.2.3.4:5001
SNI = 2.sni.example.com:5000 -> 1.2.3.4:5002
anything else on port 5000 -> 1.2.3.4:5003
haproxy.cfg:
global
log stdout format raw local0 info
defaults
timeout client 30s
timeout server 30s
timeout connect 5s
option tcplog
frontend tcp-proxy
bind :5000 ssl crt combined-cert-key.pem
mode tcp
log global
tcp-request inspect-delay 5s
tcp-request content accept if { req_ssl_hello_type 1 }
use_backend bk_sni_1 if { req.ssl_sni -i 1.sni.example.com }
use_backend bk_sni_2 if { req.ssl_sni -i 2.sni.example.com }
default_backend bk_default
backend bk_sni_1
mode tcp
log global
balance roundrobin
server server1 1.2.3.4:5001 check
backend bk_sni_2
mode tcp
log global
balance roundrobin
server server1 1.2.3.4:5002 check
backend bk_default
mode tcp
log global
balance roundrobin
server server1 1.2.3.4:5003 check
[combined-cert-key.pem
是自签名证书文件加密钥,其中CN是服务器的IP(1.2.3.4
),并且具有所有DNS值和IP的SAN。
使用openssl s_client
发起的连接:
我尝试通过DNS(A和CNAME记录以及IP)进行连接:
echo test | openssl s_client -connect demo.sni.example.com:5000 -servername 1.sni.example.com
echo test | openssl s_client -connect 1.sni.example.com:5000 -servername 1.sni.example.com
echo test | openssl s_client -connect 1.2.3.4:5000 -servername 1.sni.example.com
但是,所有连接都路由到默认后端bk_default
。
什么使HAProxy无法识别SNI服务器名? (我正在使用最新的HAProxy docker映像:https://hub.docker.com/_/haproxy)
答案是使用ssl_fc-sni,而不是req.ssl_sni
。前者用于SSL终止的会话,而后者用于TCP直接通过的会话。