【问题标题】:Using domain instead of IP in haproxy backend在 haproxy 后端使用域而不是 IP
【发布时间】:2015-12-15 17:20:45
【问题描述】:

我有 2 个虚拟主机

app.example.com:80 在 IP 地址xxx.xxx.xxx.xxx

app2.example.com:80 在 IP 地址yyy.yyy.yyy.yyy

我的 haproxy ipaddress 是sss.sss.sss.sss

这是 haproxy 配置:

global
   log 127.0.0.1 local0 notice
   maxconn 2000
   user haproxy
   group haproxy

defaults
  log     global
  mode    http
  option  httplog
  option  dontlognull
  retries 3
  option redispatch
  timeout connect  5000
  timeout client  10000
  timeout server  10000

frontend www-http
    mode http
    bind *:80
    default_backend appname
    stats enable
    stats uri /haproxy?stats
    stats auth admin:password
    stats show-node

backend appname
  balance roundrobin
  option httpclose
  option forwardfor
  server lamp1 app.example.com:80 check
  server lamp2 app2.example.com:80 check

当尝试使用 haproxy ipaddress 访问时,网络浏览器返回 xampp 仪表板而不是后端内容。

如何让 haproxy 重定向到后端内容?

【问题讨论】:

  • 在后台加http-request set-header Host app.example.com或许?

标签: networking haproxy


【解决方案1】:

我确实相信现在 1.6 中提供了该功能, http://blog.haproxy.com/2015/10/14/whats-new-in-haproxy-1-6/

您已配置的内容将简单地在实例之间对请求进行负载平衡:

server lamp1 app.example.com:80 check
server lamp2 app2.example.com:80 check

如果它们是 2 个单独的应用,请尝试:

frontend www-http
mode http
bind sss.sss.sss.sss:80
stats enable
stats uri /haproxy?stats
stats auth admin:password
stats show-node

acl app01 hdr(Host) -i app.example.com
acl app02 hdr(Host) -i app02.example.com

use_backend app01 if app01
use_backend app02 if app02

backend app01
balance roundrobin
option httpclose
option forwardfor
server lamp1 xxx.xxx.xxx.xxx:80 check

backend app02
balance roundrobin
option httpclose
option forwardfor
server lamp2 yyy.yyy.yyy.yyy:80 check

如果您现在使用 app.example.com 访问您的 haproxy,您将被转发到 lamp1,而 app2.example.com 会将您带到 lamp2

如果您想将所有内容转发到后端的 ip 并且不关心额外的匹配和映射,那么 id 使用直接监听,而不是前端:

 listen SOMENAME sss.sss.sss.sss:80
 balance leastconn
 mode http
 server lamp1 xxx.xxx.xxx.xxx:80
 server lamp2 yyy.yyy.yyy.yyy:80

【讨论】:

  • 我的意思是如果我使用 sss.sss.sss.sss,我如何将连接转发到后端?
  • 我已经更新了我的答案,请看帖子的最后一部分,希望对您有所帮助
  • 请注意,简单地将 IP 换成主机名不会使 haproxy 响应 DNS 更改。 DNS 查询将执行一次,并保持静态。要使haproxy 发送DNS 探测并定期更新其映射表,您需要添加resolvers 部分和关键字。例如见here
【解决方案2】:

如果我没记错的话,“default_backend”期望将“backend”值作为属性,而不是“listen”。

https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#4.2-default_backend

所以我建议改成

backend appname
  balance roundrobin
  option httpclose
  option forwardfor
  server lamp1 app.example.com:80 check
  server lamp2 app2.example.com:80 check

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-31
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多