There is the mod_proxy_wstunnel
(requires Apache 2.4.5 and also have installed and enabled the mod_proxy
)
Basically in your .conf
(depends on the system, in windows usually gets the majority in httpd.conf
) remove comments from the following lines:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
In distributions based on Ubuntu should use:
sudo a2enmod proxy_wstunnel
In Debian:
su && a2enmod proxy_wstunnel
But about distros this is something that we can’t address because each one has its own way of managing this, some have to install third-party resources, so the answer would be very extensive.
It is worth remembering that the ProxyPass
does not work in .htaccess
(https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass)
So basically in a Vhost or Directory you can add proxypass to your "websocket application", for example (for HTTP or HTTPS access:
ProxyPass "/ws2/" "ws://localhost:9001/"
If for HTTPS access:
ProxyPass "/wss2/" "ws://localhost:9001/"
The ws2
and the wss2
are "virtual" paths for use, can give the name you want.
In the examples I started a Node.js script that generates a local websocket on port 9001, then I downloaded the site https://www.websocket.org/echo.html and I got the result:
CONNECTED
RECEIVED: something
There are modules for older versions of Apache, but they were not official and we usually had to compile by ourselves, being Windows this was a little more problematic because it had to compile with the same version of the compiler used to compile Apache already installed.
Serving HTTP and Websocket in the same URL
In the previous example Websocket will be available with the path of a folder, if access http://localhost/ws2/
and http://localhost/wss2/
will get error 500, in case could "prevent" access so requiring the following request headers:
Connection: Upgrade
Upgrade: websocket
Then using RewriteCond
can check if the request comes from a Websocket and so directs to the "local WS server":
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:9001/$1" [P,L]
In this case you did not need to configure ProxyPass
, I believe that internally this is "implicit" (at least in the tests I performed). It is worth remembering that this also will not work in .htaccess
For those interested, the configuration for Nginx is very simple: http://nginx.org/en/docs/http/websocket.html
– Guilherme Nascimento