使用 haproxy 使用子目录的反向代理设置

reverse proxy setup using subdirs using haproxy

我在 ubuntu 16 上使用 haproxy 1.6.3 运行 让我的反向代理设置工作时遇到问题。 这是我想要实现的目标:

这里是相关配置:

我目前的issues/questions:

奖金问题:

在这里回答我自己的问题: 经过一些研究,这个用例的配置中的模式 tcp 错误,通过将前端和后端的模式切换为 http 很容易解决。 来自 docs

  • mode tcp:

In this mode, HAProxy doesn’t decipher the traffic. It just opens a TCP tunnel between the client and the server and let them together negotiate and handle the TLS traffic.

使用此模式时,HAProxy 不会评估数据包中的 HTTP headers。在这种情况下,显然没有选项可以区分 uri 等 http 特定 header 的后端,这就是初始配置不起作用的原因。

  • mode http:

In this mode, HAProxy decipher the traffic on the client side and re-encrypt it on the server side. It can access to the content of the request and the response and perform advanced processing over the traffic.

在这种情况下,所有 http header 字段都可供 haproxy 用于后端选择。

这当然对 ssl 有影响 - 此设置有多种变体,我选择使用 SSL/TLS 卸载并让 HAProxy 在客户端解密流量并以明文方式连接到内部服务器.

这使得机器 运行 成为 sslendpoint 的 haproxy,并且需要在此处而不是在网络服务器上设置 ssl 证书。此外,通过此设置,网络服务器 运行 网络应用程序可以完全隔离,仅在内部为 haproxy 机器提供页面。这也回答了问题 2。

最后,对于奖金问题:

  • 我已经通过 shell 脚本实现 'dynamic' 配置,当新机器连接到数据库中的信息时,这些脚本会即时修改 haproxy 配置,使更改生效 service haproxy reload (ubuntu) - 这似乎工作得很好。
  • 对于用户身份验证,我已将其设置为后端机器现在查询主 Web 应用程序以获取授权并在授权被拒绝时重定向到主 Web 应用程序。为了验证,使用 cookie(或不存在)。我目前正在测试此设置,但现在看来可以使用。

最后,我得到的(有效的)配置(请注意,我还在 haproxy 上添加了 http 到 https 重定向):

frontend http-in
    bind <external-ip>:80
    bind <external-ip>:443 ssl crt /path/to/cert/cert.pem
    acl machine1 path_beg /machine1
    reqadd X-Forwarded-Proto:\ https
    mode http
    option httplog
    use_backend machine1-backend if machine1
    default_backend default-backend

backend default-backend
    redirect scheme https if !{ ssl_fc }
    server main 127.0.0.1:80
    mode http

backend machine1-backend
    http-request set-header Orig-Path /machine1/
    http-request set-header X-Script-Path /machine1/
    http-request set-header Host bar.com
    reqirep ^([^\ :]*)\ /machine1/(.*)  \ /
    server m1 10.0.0.4:8081