Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortenege/91ec6fe02dca6f736303a00f8cea2731 to your computer and use it in GitHub Desktop.
Save mortenege/91ec6fe02dca6f736303a00f8cea2731 to your computer and use it in GitHub Desktop.
WebSockets using Apache Reverse Proxy

WebSockets using Apache Reverse Proxy

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.

Step 1

Clone or download a simple websocket server into our vagrantfolder (and extract if an archive).

Step 2

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>

Step 3

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)

Step 4

Navigate to http://127.0.0.1:4567/websocket.html.

THe websocket is connected 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!

Conclusion

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment