Using HTTPS and WSS with an Apache proxy
-
Hi all,
I'm running a NodeJS server on Ubuntu 18.04 behind an apache proxy.
For anyone looking for the config for an apache proxy look no further :)
After a lot of time spent searching for a solution, this is the solution I found.*Note:
You'll need the following apache modules enabled:- ssl
- proxy
- proxy_http
- proxy_html
- proxy_wstunnel
(or just run 'sudo a2enmod ssl proxy proxy_http proxy_html proxy_wstunnel')
<VirtualHost *:80> ServerName servername.xyz #redirect all requests received from port 80 to the HTTPS variant (force ssl) RewriteEngine On RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L] </VirtualHost> <VirtualHost *:443> ServerName servername.xyz #enable SSL SSLEngine On SSLCertificateFile /PATH/TO/CERT/FILE (note: i'm using the fullchain.pem file which is generated using LetsEncrypt's certbot cli) SSLCertificateKeyFile /PATH/TO/PRIVATE/KEY/FILE #setup the proxy to forward websocket requests properly (note: this proxy automatically converts the secure websocket (wss) #to a normal websocket and vice versa, so there's no need to change the colyseus library or the server for that matter) RewriteEngine On RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR] RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC] RewriteRule .* ws://127.0.0.1:APP-PORT-HERE%{REQUEST_URI} [P,QSA,L] #setup the proxy to forward all https requests to http backend (also automatic conversion from https to http and vice versa) ProxyPass "/" "http://127.0.0.1:APP-PORT-HERE/" ProxyPassReverse "/" "http://127.0.0.1:APP-PORT-HERE/" </VirtualHost>
I hope this post can be of any help :)