Haproxy& Tomcat导致随机502 Bad Gateway

时间:2016-03-02 13:28:38

标签: tomcat load-balancing haproxy

我们正在使用Tomcat 8和HAProxy 1.5.4。我们从Haproxy获得随机的502 Bad Gateway错误。当我们直接访问Tomcat时,错误没有显示出来,因此它必须与Haproxy的配置方式有关。

任何指针都会非常感激!请在下面找到错误和配置文件。谢谢!

错误日志:

  

3月1日11:41:37 www1 haproxy [15362]:xx.xx.xx.xx:56387   [01 / Mar / 2016:11:41:35.480] https-in~servers / www1a 1987/0/0 / -1 / 2029   502 8878 - - PH-- 1764/1758/46/26/0 0/0" POST   / abc / test / b25766378a05446496645649e2ddaf7a / poll HTTP / 1.1"

Tomcat HTTP连接器配置

<Connector 
    URIEncoding             = "UTF-8"
    port                    = "8080" 
    protocol                = "HTTP/1.1" 
    maxThreads              = "1850"
    connectionTimeout           = "900000"
    keepAliveTimeout            = "900000"
    maxKeepAliveRequests            = "-1"/>

Haproxy配置

global
   log /dev/log local0
   log /dev/log local1 notice
   chroot /var/lib/haproxy
   stats socket /run/haproxy/admin.sock mode 777 level admin
   stats timeout 30s
   user haproxy
   group haproxy
   daemon

   # Per process limit: The default is 2000, too small for us
   maxconn 18000

   # Increase the cache from 20000 (default), higher values reduce CPU usage
   tune.ssl.cachesize 60000

   # 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).
   ssl-default-bind-ciphers kEECDH+aRSA+AES:kRSA+AES:+AES256:!kEDH:!LOW:!EXP:!MD5:!aNULL:!eNULL
   ssl-default-bind-options no-sslv3 no-tls-tickets

defaults
   log  global
   mode http
   option   httplog
   option  http-server-close
   option  forwardfor
   option   dontlognull

   # Set the listen limit: The default is 2000, too small for us
   maxconn 9000

   # we should fix this
   option accept-invalid-http-response
   option accept-invalid-http-request
   no option checkcache

   timeout connect 80000
   timeout client  900000
   timeout server  500000

 frontend http-in
   bind *:80


    # Redirect all subdomains to www.
    redirect prefix https://www.example.com code 301 if !{ hdr_beg(host) -i     www. }

    # Redirect all trafic to https 
    redirect scheme https if !skip_pages !{ ssl_fc }

    default_backend servers

  frontend https-in

    # add no-tlsv10 for disabling tls 1.0
    bind *:443 ssl  crt /etc/ssl/private/www_example_com.pem


    default_backend servers

    # Redirect all subdomains to www.
    redirect prefix https://www.example.com code 301 if !{ hdr_beg(host) -i www. }

    backend servers

    # Every connection is closed and opened to the server
    option http-server-close

    # Recommended to enable
    option http-pretend-keepalive

    # The url to check the backend servers health
    option httpchk GET /srvstatus.htm

    # Balancing
    balance roundrobin
    appsession JSESSIONID len 52 timeout 3h request-learn prefix
    stick-table type string len 32 size 1M expire 3h

    # We have 3 backend servers, one is for backup
    server www1a 127.0.0.1:8080 check
    server www2a xx.xx.xx.xx:8080 check
    server www1b 127.0.0.1:8081 check  backup

1 个答案:

答案 0 :(得分:1)

我遇到了与tomcat和haproxy相同的问题。 Tomcat正在响应一个http 200响应,但haproxy代理它并以502响应。 原因在于haproxy限制:

tune.bufsize 1638400    tune.http.maxhdr 10000

我将这两个属性更改为高值。默认值是限制性的,并将来自后端的http 200响应转换为502响应。 请注意这两个属性的高值,因为您很容易出现内存问题。 一旦我改变了这个属性,502的问题就结束了。