【问题标题】:GCP External HTTP(S) Load Balancer Returns 502: "backend_connection_closed_before_data_sent_to_client"GCP 外部 HTTP(S) 负载均衡器返回 502:“backend_connection_closed_before_data_sent_to_client”
【发布时间】:2021-09-18 13:27:21
【问题描述】:

我在 GCP 上的 HTTP(S) 外部负载均衡器偶尔会返回带有错误代码 502 的响应。

并且响应的原因如下:

jsonPayload: {
@type: "type.googleapis.com/google.cloud.loadbalancing.type.LoadBalancerLogEntry"
statusDetails: "backend_connection_closed_before_data_sent_to_client"
}

根据 GCP 文档,出现此类响应的原因如下:

后端意外关闭了与负载均衡器的连接 在将响应代理给客户端之前。如果 负载均衡器正在向另一个实体发送流量。另一个实体 可能是第三方负载均衡器,其 TCP 超时为 比外部 HTTP(S) 负载平衡器的 10 分钟短 (600 秒)超时。第三方负载均衡器可能正在运行 在虚拟机实例上。手动设置 TCP 超时(保持连接) 超过 600 秒的目标服务可能会解决此问题。

Reference.

在我的负载均衡器的后端,我有一个 GCP 虚拟机,它运行具有以下配置的 HAProxy 服务器 (v1.8):

global
    log         /dev/log    local0
    log         /dev/log    local1 notice
    chroot      /var/lib/haproxy
    pidfile     /var/run/rh-haproxy18-haproxy.pid

    user        haproxy
    group       haproxy
    daemon
    stats       socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners

    spread-checks  21

    # Default SSL material locations
    ca-base     /etc/ssl/certs
    crt-base    /etc/ssl/private

    # Default ciphers to use on SSL-enabled listening sockets.
    # For more information, see ciphers(1SSL). This list is from:
    #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
    # An alternative list with additional directives can be obtained from
    #  https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
    ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
    ssl-default-bind-options no-sslv3

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option                  http-server-close
    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                 10000
    balance                 roundrobin


frontend http-80
        bind *:80
        mode http
        option httplog
        default_backend www-80

backend www-80
        balance roundrobin
        mode http
        option httpchk /haproxy_status
        http-check expect status 200
        rspidel ^Server:.*
        rspidel ^server:.*
        rspidel ^x-envoy-upstream-service-time:.*
        server backendnode1 node-1:80 check port 8080 fall 3 rise 2 inter 1597
        server backendnode2 node-2:80 check port 8080 fall 3 rise 2 inter 1597
        

frontend health-80
    bind *:8080
    acl backend_dead nbsrv(www-80) lt 1
    monitor-uri /haproxy_status
    monitor fail if backend_dead

listen stats # Define a listen section called "stats"
    bind :9000 # Listen on localhost:9000
    mode http
    stats enable  # Enable stats page
    stats hide-version  # Hide HAProxy version
    stats realm Haproxy\ Statistics  # Title text for popup window
    stats uri /haproxy_stats  # Stats URI
    stats auth haproxy:pass  # Authentication credentials
    
#lastline

根据 GCP 文档,我们可以通过设置高于 600 秒(10 分钟)的 TCP Keep-Alive 值来消除 502 错误。

他们为 Apache 和 Nginx 提供了建议值。

Web server software  Parameter          Default setting         Recommended setting
Apache               KeepAliveTimeout   KeepAliveTimeout 5      KeepAliveTimeout 620
nginx                keepalive_timeout  keepalive_timeout 75s;  keepalive_timeout 620s;

Reference.

我不确定我应该在我的 HAProxy 配置中更改什么超时值或什么配置以将 keepalive 时间设置为超过 600 秒。

timeout http-keep-alive 设置为超过 600 秒是否有效?

【问题讨论】:

    标签: google-cloud-platform load-balancing haproxy


    【解决方案1】:

    您的 HAProxy 版本应该默认启用 keep-alive 选项,但我在您的配置文件中没有看到相应的行;

    要启用它,您需要在 defaults 部分添加 option http-keep-alive 行,使其看起来像这样:

    defaults
        mode                    http
        log                     global
        option                  httplog
        option                  dontlognull
        option                  http-server-close
        option                  redispatch
        retries                 3
        option                  http-keep-alive
    

    要检查它是否正常工作,请按照this anwer 的说明进行操作。

    您还可以在 SO 上找到有用的这些主题:

    How to make HA Proxy keepalive

    How to enable keep-alive in haproxy?

    【讨论】:

    • @Wojtek_B option http-keep-alive 部分中的设置 default 似乎不起作用。我怀疑这可能是因为我已经在那里设置了option http-server-close。我需要进一步验证。
    • 你试过用option http-keep-alive代替option http-server-close吗?
    猜你喜欢
    • 2018-10-05
    • 2021-08-31
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2018-04-14
    • 2021-12-24
    • 2020-10-07
    相关资源
    最近更新 更多