首页 > 解决方案 > 如何使用 HA 代理服务器作为负载均衡器

问题描述

我正在尝试使用 HA 代理服务器作为我的两个 Web 服务器之间的负载平衡器。各个服务器已启动并在指定端口上运行。但我无法通过 HA 代理服务器访问 Web 服务器。我已经使用标准设置配置了 HA 代理,如下所示

/etc/haproxy/haproxy.cfg

global
    log         127.0.0.1 local0

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
    mode http
    default_backend             app

backend app
    balance     roundrobin
    option forwardfor
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }
    option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    server  app1 10.368.240.116:9091
    server  app2 10.368.240.317:9092
    #server  app3 127.0.0.1:5003 check
    #server  app4 127.0.0.1:5004 check

我可以从我的 HA 代理主机成功地卷曲上述服务器

curl -i "10.368.240.116:9091"
HTTP/1.1 200 OK

curl -i "10.368.240.317:9091"
HTTP/1.1 200 OK

我的 HA 代理服务器正在侦听 80 端口但是当我点击它时,它给出了 503 错误,如下所示

curl -iv  "127.0.0.1"
* About to connect() to 127.0.0.1 port 80 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 503 Service Unavailable
HTTP/1.0 503 Service Unavailable
< Cache-Control: no-cache
Cache-Control: no-cache
< Connection: close
Connection: close
< Content-Type: text/html
Content-Type: text/html

<
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
* Closing connection 0

谁能指导我在哪里做错了

标签: load-balancinghaproxy

解决方案


推荐阅读