HAproxy:在后端重定向到 https

HAproxy: Redirect to https in backend

我的工作场所有一个 HAproxy,我们用它来路由到只需要一个 public IP 的网络服务器。我们的一些客户需要 https,有些则不需要。

我想在每个后端基础上强制执行 https。

我找到了这个,只是没有说明这个配置是用于前端还是后端。也许它对两者都有效?

http-request redirect location [code ] [] []

或者这个:

mode http

redirect scheme https if !{ ssl_fc }

所以我想我把这个放在一些后端:

http-request redirect location https://www.somedomain.com [code 301]

这行得通吗?我们的实验室环境。被捆绑所以我不能及时测试它。

我创建了自己的测试后端.. 这有效:

backend lb_customername
          mode http
          redirect scheme https if !{ ssl_fc }

          balance roundrobin

          server server1 10.0.0.51:80 maxconn 200
          server server2 10.0.0.52:80 maxconn 200

来自the HAProxy documentation for redirect scheme

May be used in sections
defaults    no
frontend    yes
listen      yes
backend     yes

所以这会起作用(从工作部署中复制)

backend https_for_all_traffic
    redirect scheme https if !{ ssl_fc }

    server https_only 10.21.5.73:80

由于 !{ ssl_fc } 检查本质上只是另一个 ACL,您甚至可以将它与其他 ACL 组合并仅转发特定流量:

backend https_for_some_traffic
    # Detect traffic to admin pages
    acl secure    url_beg    /admin

    # Force any HTTP admin traffic to HTTPS
    #  the conditions are combined with an implicit AND
    redirect scheme https if !{ ssl_fc } secure

    server both_http_and_https 10.21.5.73:80