This post will cover how to configure our already running server [1, 2] to proxy websocket connections to a backend of our choice. We will continue using Vagrant to provision the guest machine.
Clone or download a simple websocket server into our vagrantfolder (and extract if an archive).
Modify 001-mysite.conf
to include our websocket redirect.
<VirtualHost *:80>
ProxyPass "/ws/" "ws://127.0.0.1:8000/"
ProxyPassReverse "/ws/" "ws://127.0.0.1:8000/"
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
</VirtualHost>
Modify bootstrap.sh
to 1) enable mod_proxy_wstunnel and 2) start a websocket server.
[...]
a2enmod proxy_wstunnel
[ before SimpleHTTPServer ]
cd /vagrant/www/html
[...]
cp /vagrant/simple-websocket-server-master/SimpleWebSocketServer/websocket.html /vagrant/www/html
python /vagrant/simple-websocket-server-master/SimpleWebSocketServer/SimpleExampleServer.py --example echo &> /dev/null &
When everything is configured reprovision the guest machine (vagrant up or vagrant reload --provision depending)
Navigate to http://127.0.0.1:4567/websocket.html.
As seen in the image above, connect the browser to
ws://127.0.0.1:4567/ws/
Click connect and send and review the output.
connected
sent: Hello World!
response: Hello World!
We have successfully configured our Apache server to function as a reverse proxy that allows us to route traffic to different backends depending on the relative path.