Configure Apache + Tomcat with Websocket capability

If you configure Apache httpd server and Tomcat server together, you usually use Tomcat connect(AJP) to hook up both of them. Http requests and responses are transferred through the AJP tunnel. AJP works well on HTTP protocols. However, if you have to invoke websocket calls(ws://xxx.com/yyy/zzz), AJP fails to complete it.

To resolve it, we configure wstunnel between Apache httpd and Tomcat.

Here we take httpd-2.4.25 as an sample.

Step 1. Prepare mod_proxy_wstunnel.so file
By default, Apache HTTPD doesn’t provide mod_proxy_wstunnel.so file(/usr/local/apache-2.4.25/modules/mod_proxy_wstunnel.so). We have to build it ourselves.
Download httpd-2.4.25 and uncompress it.

cd /aaa/bbb/httpd-2.4.25-src
./configure --prefix=/usr/local/apache-2.4.25 --enable-mods-shared=all --enable-so --enable-proxy=shared --enable-proxy-wstunnel
make
make install

–enable-proxy=shared –enable-proxy-wstunnel will generate mod_proxy_wstunnel.so file.

Step 2. Modify httpd.conf
Edit /usr/local/apache-2.4.25/conf/httpd.conf

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

Step 3. Set proxy rule
Based on your requirement, edit httpd.conf or httpd-vhosts.conf
If pointing to localhost, add

ProxyPass /websocket/ ws://localhost:8080/websocket/
ProxyPassReverse /websocket/ ws://localhost:8080/websocket/

Notice. In Tomcat server.xml, there must be a connector defined as port=”8080″ protocol=”HTTP/1.1″

If pointing to another host, add

ProxyPass /websocket/ ws://host.abc.com:8080/websocket/
ProxyPassReverse /websocket/ ws://host.abc.com:8080/websocket/

Step 4. Restart httpd server

oliver

Leave a Reply

Your email address will not be published. Required fields are marked *


*