The WebSocket protocol is an extension of the HTTP protocol. However, the proxy module of Apache2 does not seem to know about it, and throws away crucial headers, converting the call to a standard HTTP call.
Is there a way to make Apache2 either (1) understand WebSocket or (2) simply blindly pass on whatever it gets?
There is now a module in the Apache trunk called mod_proxy_wstunnel which allows mod_proxy (ProxyPass/ProxyPassReverse) to pass through WebSocket traffic. Someone wrote a blog post about back-porting mod_proxy_wstunnel to Apache 2.4/2.2 and provided a patch to do so.
I figured out concrete instructions to set up mod_proxy_wstunnel on Ubuntu (tested with Ubuntu Server 11.10 and Apache 2.2.20) and posted them on my blog. I have copied them below:
There is nothing to indicate Apache httpd will support them anytime soon.
If you must run websockets through apache, try mod_pywebsocket. I have tried it, and it does work.
Here are a few alternatives I prefer:
Looks like with a combination of the disconnect plugin and some extra code this is now possible:
http://blog.alex.org.uk/2012/02/16/using-apache-websocket-to-proxy-tcp-connection/
Please take a look at http://github.com/disconnect/apache-websocket
This add's to @Andrew Moss' answer as to how to correctly configure the
VirtualHost
to work with socket.io 1.0! Feel free to skip the part about CentOS!If you're stuck on CentOS 6, here is how to do it:
mod_proxy_wstunnel
module here (either clone the Gist or download the files individually)yum install make gcc httpd-devel
.c
-file into theSOURCES
subfolder of the environment and the.spec
-file into theSPECS
subfolder.rpmbuild -ba mod_proxy_wstunnel.spec
SRPMS
subfolderrpm -i /path/to/package.rpm
This will also automatically load the module in Apache, so you just have to restart it with
service httpd restart
.Setting up a
VirtualHost
to actually serve the Socket.io server and client-script (which is by default available underhttp://your.server/socket.io/socket.io.js
) is a bit more complicated on Apache 2.2, because of a Bug in themod_proxy
module:So, you can't use the
ws
-protocol in a rewrite-rule, because that will internally turn into an HTTP GET request.There is a workaround though:
This makes sure that everything sent to
/socket.io
goes to thews://
-protocol, except the request for long-polling (which is a fallback mechanism when WebSockets are not available) and the request for the client-library.