502 Proxy Error Using CometD, Apache and Camel
Join the DZone community and get the full member experience.Join For Free
We recently hit an issue during testing with Apache returning "502 Proxy
Errors" to clients who were connecting via CometD (using Apache as a
proxy server in front of a Camel/Jetty CometD server).
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>502 Proxy Error</title> </head><body> <h1>Proxy Error</h1> <p>The proxy server received an invalid response from an upstream server. The proxy server could not handle the request <em><a href="/cometdServer/connect">POST /cometdServer/connect</a></em>.<p> Reason: <strong>Error reading from remote server</strong></p></p> </body></html>
Our SetupWe are using a combination of CometD, Camel (in ActiveMQ) and Apache to broadcast messages from our server to subscribed browser clients.
- CometD is a scalable HTTP-based event routing bus that uses an Ajax Push technology pattern known as Comet.
- Comet is a web application model using long-held HTTP requests which
allow the web server to ‘PUSH’ data to a browser, without the browser
specifically requesting it.
- We have Camel sitting on an ActiveMQ server. On this we have routes
set up to process incoming JMS messages. These messages get processed,
and then the output from this process (in the form of a JSON message) is
sent to a CometD endpoint defined in the Camel route, for example:
- This endpoint uses the Camel CometD module to run a Jetty instance on port 9099, which handles the CometD endpoint.
- The browser clients connect to this endpoint via an Apache server. We do this to make use of Apaches mod_proxy module to act as a proxy to the CometD endpoint, e.g. in proxy.conf:
ProxyPass /cometdServer http://camelserver:9099/cometd ProxyPassReverse /cometdServer http://camelserver:9099/cometd
- This allows us to easily ‘PUSH’ messages out to subscribed browser clients to inform them of the results of processing taking place inside our Camel messaging routes (the kind of thing you would typically implement using JMS Topics in a java desktop/swing type solution)
Known Problems with Apache and CometD Long PollingAs a general rule, using Apache as a proxy in front of a CometD long-polling/Bayeux server is not a good idea. This is due to Apaches ‘thread-per-request’ model – which introduces problems when scaling this solution.
This is described in more detail here:
The problem is caused by the Apache proxy server timing out before the Jetty\Camel server (using the default configuration). By default, Apache Timeouts are set to 2 minutes (as defined globally in httpd.conf).
Over on the Camel\CometD side, the default timeout is set to 4 minutes (240000 milliseconds)
Under certain conditions – if the CometD endpoint holds the connection for longer than 2 minutes, it causes a timeout on the Apache proxy, which then disconnects the CometD connection on the client (the length it holds the connection open seems to vary, but is typically much shorter than this)
Solution 1Increase the timeout on the Apache Proxy server to be greater than 4 minutes. You can either do this globally, or set it specifically on each ProxyPass configuration (in proxy.conf)
ProxyPass /cometdServer http://camelserver:9099/cometd timeout=250 ProxyPassReverse /cometdServer http://camelserver:9099/cometd timeout=250
Solution 2Decrease the timeout on the Camel server to be less than 2 minutes, e.g.
Useful Tools for DebuggingI found these tools invaluable in getting to the bottom of what was happening (and digging into the details of the CometD messages)
Chrome Developer ToolsUseful for monitoring the network traffic between the browser and the server – lets you easily see the details of each HTTP interaction (on the ‘Network’ tab). Of particular use here was seeing the Timings – you could see that the request which failed did so at a 2 minutes point
Apache Server-Status ModuleEnabling apache mod_status was useful to confirm that the problem was not thread related (as mentioned in the known issues with Apache/CometD above)
This lets you view what the current worker threads are doing on Apache. Instructions on how to enable can be found here:
Published at DZone with permission of Adrian Milne, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.