WebSocket through SSL with Apache reverse proxy WebSocket through SSL with Apache reverse proxy apache apache

WebSocket through SSL with Apache reverse proxy


I ended up solving this problem by using this configuration for the virtual host, which filters requests using the HTTP headers:

<VirtualHost *:443>    ServerName website.com    RewriteEngine On    # When Upgrade:websocket header is present, redirect to ws    # Using NC flag (case-insensitive) as some browsers will pass Websocket    RewriteCond %{HTTP:Upgrade} =websocket [NC]    RewriteRule ^/ws/(.*)    wss://localhost:8888/ws/$1 [P,L]    # All other requests go to http    ProxyPass "/" "http://localhost:8888/"

I'm leaving this as a reference in case it helps others


This is my setup of virtualhost that worked for me, I have .netcore app on docker with SignalR as a websocket service.

On 5000 my .netcore app is running, and on /chatHub my signalR listens.

Will be helpful for future comers with same problem.

<IfModule mod_ssl.c><VirtualHost *:443>  RewriteEngine On  ProxyPreserveHost On  ProxyRequests Off  # allow for upgrading to websockets  RewriteEngine On  RewriteCond %{HTTP:Upgrade} =websocket [NC]  RewriteRule /(.*)           ws://localhost:5000/$1 [P,L]  RewriteCond %{HTTP:Upgrade} !=websocket [NC]  RewriteRule /(.*)           http://localhost:5000/$1 [P,L]  ProxyPass "/" "http://localhost:5000/"  ProxyPassReverse "/" "http://localhost:5000/"  ProxyPass "/chatHub" "ws://localhost:5000/chatHub"  ProxyPassReverse "/chatHub" "ws://localhost:5000/chatHub"  ServerName site.com  SSLCertificateFile /etc/letsencrypt/live/site.com/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/site.com/privkey.pemInclude /etc/letsencrypt/options-ssl-apache.conf</VirtualHost></IfModule>

Source: http://shyammakwana.me/server/websockets-with-apache-reverse-proxy-with-ssl.html


@ pimgeek's Comment:

I think instead ofRewriteRule ^/nodered/comms wss://localhost:1880/nodered/comms [P,L]

you could have utilized $1 as follow:RewriteRule ^/nodered/comms$ wss://localhost:1880/$1 [P,L]

Also, this should work aswell:RewriteRule ^/nodered/comms$ wss://localhost:1880$1 [P,L]

Notice the not needed / after the port, since $1 includes already a / at the beginning