In this guide are going to install and configure Apache, mod_proxy and proxy_wstunnel. proxy_wstunnel is required if you want successfully connect to WildFly via websockets (thru Apache). Any attempt to connect via websockets without proxy_wstunnel will fail as the HTTP Upgrade headers will be removed from your request.
Apache install
First install Apache and the required mods:sudo apt-get install apache2 sudo apt-get install libapache2-mod-proxy-html sudo a2enmod proxy_http sudo a2enmod proxy_wstunnel sudo /etc/init.d/apache2 restart
Apache configuration
Now configure your VirtualHost. For simplicity we are going to update the 000-default.conf file. In this example the context root of the application is app. Update yours as needed.vim /etc/apache2/sites-available/000-default.conf
These two lines are required to proxy your main http connections
ProxyPass /app http://localhost:8080/pss ProxyPassReverse /app http://localhost:8080/pss
These two lines will proxy your websocket connections
ProxyPass /app/ws/ ws://localhost:8080/app/ws/ ProxyPassReverse /app/ws/ ws://localhost:8080/app/ws/
Here is the full VirtualHost file, with the mod_proxy and proxy_wstunnel configuration highlighted:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com
ProxyPass /app http://localhost:8080/pss
ProxyPassReverse /app http://localhost:8080/pss
ProxyPass /app/ws/ ws://localhost:8080/app/ws/
ProxyPassReverse /app/ws/ ws://localhost:8080/app/ws/
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
Reload Apache for the changes to take effectsudo /etc/init.d/apache2 reload
Java configuration
Make sure that the value you pass into your @ServerEndPoint annotation matches that in your Apache VirtualHost configuration.@ServerEndpoint("/ws")
public class WebSocketEndpoint {
private final Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onConnectionOpen(Session session) {
logger.info("Connection opened");
}
@OnMessage
public String onMessage(String message) {
return "Message received";
}
@OnClose
public void onConnectionClose(Session session) {
logger.info("Connection closed");
}
}
WildFly configuration
Make sure that you allow connections to your public interface from localhost (127.0.0.1) only. While this is not mandatory, leaving it open will allow connections to circumvent Apache.<interfaces>
<interface name="management">
<inet-address value="0.0.0.0"/>
</interface>
<interface name="public">
<inet-address value="${jboss.bind.address:127.0.0.1}"/>
</interface>
<interface name="unsecure">
<inet-address value="${jboss.bind.address.unsecure:127.0.0.1}"/>
</interface>
</interfaces>
In WildFly websockets work out of the box, so no other configuration required. Here is the default undertow configuration, with the relevant parts highlighted.<subsystem xmlns="urn:jboss:domain:undertow:1.2"> <buffer-cache name="default"/> <server name="default-server"> <http-listener name="default" socket-binding="http"/> <host name="default-host" alias="localhost"> <location name="/" handler="welcome-content"/> <filter-ref name="server-header"/> <filter-ref name="x-powered-by-header"/> </host> </server> <servlet-container name="default"> <jsp-config/> <websockets/> </servlet-container> </subsystem>You should now be able to deploy your websocket applications to WildFly and access them via Apache.