【问题标题】:502 Bad Gateway HAproxy502 错误网关 HAproxy
【发布时间】:2013-09-08 00:52:30
【问题描述】:

我正在运行 Ubuntu 12.04LTS。我的网络服务器是 Tomcat 7.0.42,我使用 HAProxy 作为代理服务器。我的应用程序是一个使用 websockets 的 servlet 应用程序。

有时,当我请求我的页面时,我在某些资源上收到“502 Bad Gateway”错误,但不是全部,而是某些。我认为这与我的 HAProxy 配置有关,如下所示:

global
    maxconn     4096 # Total Max Connections. This is dependent on ulimit
    nbproc      1

defaults
    mode        http
    option  http-server-close
    option httpclose
#   option  redispatch
    no option checkcache  # test against 502 error

frontend all 0.0.0.0:80
    timeout client 86400000
    default_backend www_backend
    acl is_websocket hdr(Upgrade) -i WebSocket
    acl is_websocket hdr_beg(Host) -i ws

    use_backend socket_backend if is_websocket

    backend www_backend
        balance roundrobin
        option forwardfor # This sets X-Forwarded-For
        timeout server 30000
        timeout connect 4000
        server apiserver localhost:8080 weight 1 maxconn 1024 check

    backend socket_backend
        balance roundrobin
        option forwardfor # This sets X-Forwarded-For
        timeout queue 5000
        timeout server 86400000
        timeout connect 86400000
        server apiserver localhost:8080 weight 1 maxconn 1024 check

为了防止 502 错误,我必须进行哪些更改?

【问题讨论】:

    标签: linux tomcat ubuntu proxy haproxy


    【解决方案1】:

    首先,启用 haproxy 日志记录。它会简单地告诉你它为什么给出 502 的。我的猜测是后端“localhost:8080”根本无法跟上或无法在 4000 毫秒“超时连接 4000”内获得连接。

    【讨论】:

      【解决方案2】:

      您可能超出了 HAProxy 中的一些默认限制。尝试将以下内容添加到全局部分:

      tune.maxrewrite 4096
      tune.http.maxhdr 202
      

      【讨论】:

      • 您能否详细说明为什么建议的配置更改解决了 OP 的问题?
      • 请在答案中添加更多详细信息。当前表格中的答案可能会被删除。
      【解决方案3】:

      你应该用这些替换你的默认值:

      # Set balance mode
      balance random
      # Set http mode
      mode http
      # Set http keep alive mode (https://cbonte.github.io/haproxy-dconv/2.3/configuration.html#4)
      option http-keep-alive
      # Set http log format
      option httplog
      # Dont log empty line
      option dontlognull
      # Dissociate client from dead server
      option redispatch
      # Insert X-Forwarded-For header
      option forwardfor
      

      不要使用 http-server-close,这很可能是你的问题的原因。

      Keep-alive 将与双方的客户端和服务器建立连接。 它也适用于 websockets。

      如果您在服务器上启用检查,您还需要使用以下内容对其进行配置:

      # Enable http check
      option httpchk
      # Use server configuration
      http-check connect default
      # Use HEAD on / with HTTP/1.1 protocol for Host example.com
      http-check send meth HEAD uri / ver HTTP/1.1 hdr Host example.com
      # Expect status 200 to 399
      http-check expect status 200-399
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 2021-07-21
        相关资源
        最近更新 更多