You need to enable JavaScript to run this app. HAProxy配置教程:单域名SSL交由后端处理其余由前端处理的实现方法
优惠活动
大模型
产品
解决方案
定价
更多

HAProxy配置教程:单域名SSL交由后端处理其余由前端处理的实现方法

解决方案

核心实现思路:因为要求myotherserver.com的SSL完全由后端处理、HAProxy不做SSL终止,所以通过TCP层SNI分流的方式实现,完全不修改原有myserver.com的SSL处理逻辑,仅新增流量转发规则即可。

修改步骤

  • 将原有frontend http_in中绑定的公网443端口改为本地回环端口,保留原有SSL终止逻辑处理myserver.com相关请求
  • 新增TCP模式的公网443监听入口,通过SNI字段识别域名,将myserver.com流量转发到本地回环的SSL终止端口,myotherserver.com流量直接透传给对应后端
  • 可选新增myotherserver.com的80端口转发规则,可根据需求选择是否转发80或由后端自行处理跳转

完整修改后配置示例

# 新增TCP层443监听,做SNI分流
frontend tcp_443_in
    mode tcp
    option tcplog
    bind :443
    tcp-request inspect-delay 5s
    tcp-request content accept if { req.ssl_hello_type 1 }

    # 匹配myserver.com的SNI,转发到本地SSL终止端口
    acl is_myserver req.ssl_sni -i www.myserver.com billing.myserver.com
    # 匹配myotherserver.com的SNI
    acl is_other req.ssl_sni -i myotherserver.com www.myotherserver.com

    use_backend myserver_ssl_endpoint if is_myserver
    use_backend myotherserver_backend if is_other

# myserver.com本地SSL终止入口
backend myserver_ssl_endpoint
    mode tcp
    server local_ssl 127.0.0.1:8443

# 原有http入口,仅监听本地8443和公网80
frontend http_in
    mode http
    option httplog
    option forwardfor
    bind :80
    # 原公网443绑定改为本地回环端口
    bind 127.0.0.1:8443 ssl crt /etc/letsencrypt/live/myserver.com/haproxy.pem
    redirect scheme https if !{ ssl_fc }

    acl host_server1 hdr(host) -i www.myserver.com
    acl host_server2 hdr(host) -i billing.myserver.com
    # 新增myotherserver.com 80端口匹配规则
    acl host_other hdr(host) -i myotherserver.com www.myotherserver.com

    use_backend website_server if host_server1
    use_backend billing_server if host_server2
    use_backend myotherserver_http if host_other


backend website_server
    mode http
    option forwardfor
    option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    server web.server web.server:80 check
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }


backend billing_server
    mode http
    option forwardfor
    option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    server billing.server billing.server:80
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }

# myotherserver.com HTTPS透传后端,SSL完全由后端处理
backend myotherserver_backend
    mode tcp
    option tcp-check
    # send-proxy-v2参数用于向后端传递真实客户端IP,不需要可删除
    server other.server 你的后端服务IP:443 check send-proxy-v2

# myotherserver.com 80端口转发后端
backend myotherserver_http
    mode http
    option forwardfor
    server other.server 你的后端服务IP:80 check

注意事项

  • 确保HAProxy编译时开启了USE_OPENSSL参数,支持SNI识别
  • 透传模式下HAProxy不会修改myotherserver.com的HTTPS流量,证书更新、SSL配置完全由后端服务自行维护
  • 若不需要myotherserver.com的80端口访问,可直接删除80端口对应的ACL和转发规则

内容的提问来源于stack exchange,提问作者Daryn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 05:36:07