websocketapache2

How to allow websocket via reverse proxy on apache 2.4


I am relatively new to server configuration and can't figure out how to do it. I have a web server with a revers proxy which works:

<VirtualHost *:80>
    ServerName vtt.domain.com
    ProxyPreserveHost On

    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass / https://localhost:30000/
    ProxyPassReverse / https://localhost:30000/

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =vtt.domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

But the docker on port 30000 also requires web socket connections over the same URL, and that is the part I can't figure out.

I tried to modify it to this:

<VirtualHost *:80>
    ServerName vtt.domain.de
    ProxyPreserveHost On

    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
   RewriteCond %{HTTP:Connection} Upgrade [NC]
   RewriteCond %{HTTP:Upgrade} websocket [NC]
   RewriteRule /(.*) ws://127.0.0.1:30000/$1 [P,L]

   ProxyRequests off
   <Location />
      ProxyPass http://127.0.0.1:30000/
      ProxyPassReverse /
   </Location>
</VirtualHost>

But that does not work. The Browser still just gets WebSocket connection to 'wss://vtt.domain.de/socket.io/?session=10dce06dhf5dfcc88ca8bdbau&EIO=4&transport=websocket' failed What do I have to do to let it allow web sockets? The Server is Running on Ubuntu 22.04 and apache is on version 2.4.52


Solution

  • You are missing (in the latter example): RewriteEngine on

    I also changed the last line a bit, and got it working with:

       RewriteEngine on
       RewriteCond %{HTTP:Connection} Upgrade [NC]
       RewriteCond %{HTTP:Upgrade} websocket [NC]
       RewriteRule ^/?(.*) "ws://127.0.0.1:30000/$1" [P,L]
    

    But on Apache >= 2.4.47 you can just do:

    ProxyPass http://127.0.0.1:30000/ upgrade=websocket
    

    No need to add upgrade=websocket to ProxyPassReverse on the other hand.

    References: