Is it possible to use Websocket connections in Apache?

Asked

Viewed 221 times

5

With the emergence of Websockets in the browsers, I also had the interest to make some implementations in some applications. Of course with the idea comes the initial doubts.

One of the questions I have been asked is about Apache. Traditionally I use PHP and Apache to develop web applications.

When using Websocket, tutorials usually teach you to use a script that opens a TCP connection on a given port. So, you use Javascript to connect to this port.

Instead of creating an independent server script to run this Websocket, could I use Apache? Because I noticed that Websocket uses a status response 101 and some specific headers, which would be perfectly possible to return via Apache. However, I have doubts about Apache having resources to support Websocket.

I saw this question on Soen on the subject and I had the impression that Apache would not be indicated. If it is not really indicated, what is the reason?

What would be the most suitable tools/resources to start working with Websocket, if Apache does not serve such a purpose?

1 answer

2


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

websocket no Apache com ProxyPass e proxy_wstunnel

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.