Bad Gateway 502 error with Apache mod_proxy and Tomcat

前端 未结 10 1642
北海茫月
北海茫月 2020-12-12 16:24

We\'re running a web app on Tomcat 6 and Apache mod_proxy 2.2.3. Seeing a lot of 502 errors like this:

Bad Gateway! The proxy server received an in

相关标签:
10条回答
  • 2020-12-12 16:40

    Most likely you should increase Timeout parameter in apache conf (default value 120 sec)

    0 讨论(0)
  • 2020-12-12 16:48

    I know this does not answer this question, but I came here because I had the same error with nodeJS server. I am stuck a long time until I found the solution. My solution just adds slash or /in end of proxyreserve apache.

    my old code is:

    ProxyPass / http://192.168.1.1:3001
    ProxyPassReverse / http://192.168.1.1:3001
    

    the correct code is:

    ProxyPass / http://192.168.1.1:3001/
    ProxyPassReverse / http://192.168.1.1:3001/
    
    0 讨论(0)
  • 2020-12-12 16:50

    You can use proxy-initial-not-pooled

    See http://httpd.apache.org/docs/2.2/mod/mod_proxy_http.html :

    If this variable is set no pooled connection will be reused if the client connection is an initial connection. This avoids the "proxy: error reading status line from remote server" error message caused by the race condition that the backend server closed the pooled connection after the connection check by the proxy and before data sent by the proxy reached the backend. It has to be kept in mind that setting this variable downgrades performance, especially with HTTP/1.0 clients.

    We had this problem, too. We fixed it by adding

    SetEnv proxy-nokeepalive 1
    SetEnv proxy-initial-not-pooled 1
    

    and turning keepAlive on all servers off.

    mod_proxy_http is fine in most scenarios but we are running it with heavy load and we still got some timeout problems we do not understand.

    But see if the above directive fits your needs.

    0 讨论(0)
  • 2020-12-12 16:51

    you should be able to get this problem resolved through a timeout and proxyTimeout parameter set to 600 seconds. It worked for me after battling for a while.

    0 讨论(0)
  • 2020-12-12 16:51

    If you want to handle your webapp's timeout with an apache load balancer, you first have to understand the different meaning of timeout. I try to condense the discussion I found here: http://apache-http-server.18135.x6.nabble.com/mod-proxy-When-does-a-backend-be-considered-as-failed-td5031316.html :

    It appears that mod_proxy considers a backend as failed only when the transport layer connection to that backend fails. Unless failonstatus/failontimeout is used. ...

    So, setting failontimeout is necessary for apache to consider a timeout of the webapp (e.g. served by tomcat) as a fail (and consecutively switch to the hot spare server). For the proper configuration, note the following misconfiguration:

    ProxyPass / balancer://localbalance/ failontimeout=on timeout=10 failonstatus=50

    This is a misconfiguration because:

    You are defining a balancer here, so the timeout parameter relates to the balancer (like the two others). However for a balancer, the timeout parameter is not a connection timeout (like the one used with BalancerMember), but the maximum time to wait for a free worker/member (e.g. when all the workers are busy or in error state, the default being to not wait).

    So, a proper configuration is done like this

    1. set timeout at the BalanceMember level:
     <Proxy balancer://mycluster>
         BalancerMember http://member1:8080/svc timeout=6 
     ... more BalanceMembers here
    </Proxy>
    
    1. set the failontimeout on the balancer
    ProxyPass /svc balancer://mycluster failontimeout=on
    

    Restart apache.

    0 讨论(0)
  • 2020-12-12 16:53

    You can avoid global timeouts or having to virtual hosts by specifying the proxy timeouts in the ProxyPass directive as follows:

    ProxyPass /svc http://example.com/svc timeout=600
    ProxyPassReverse /svc http://example.com/svc timeout=600
    

    Notice timeout=600 seconds.

    However this does not always work when you have load balancer. In that case you must add the timeouts in both the places (tested in Apache 2.2.31)

    Load Balancer example:

    <Proxy "balancer://mycluster">
         BalancerMember "http://member1:8080/svc" timeout=600
         BalancerMember "http://member2:8080/svc" timeout=600
    </Proxy> 
    
    ProxyPass /svc "balancer://mycluster" timeout=600
    ProxyPassReverse /svc "balancer://mycluster" timeout=600
    

    A side note: the timeout=600 on ProxyPass was not required when Chrome was the client (I don;t know why) but without this timeout on ProxyPass Internet Explorer (11) aborts saying connection reset by server.

    My theory is that the :

    ProxyPass timeout is used between the client(browser) and the Apache.

    BalancerMember timeout is used between the Apache and the backend.

    To those who use Tomcat or other backed you may also want to pay attention to the HTTP Connector timeouts.

    0 讨论(0)
提交回复
热议问题