2.1 KiB
Reverse proxy
Introduction
A reverse proxy is for example used to host 2 services on the same port. This is useful for us because we can use the same port for the websockets and the webserver.
How to use a reverse proxy
We are going to use Apache2
as our reverse proxy. This is because it is easy to use and has a lot of documentation. It also hosts our webserver so it is easy to configure and we don't have to install another program.
Additional modules
We need to enable the proxy
and proxy_wstunnel
modules. These are used to proxy the websockets.
You can install them with these commands on a Raspberry Pi. For other linux distributions its may be different.
$ sudo a2enmod proxy
$ sudo a2enmod proxy_wstunnel
Script
The enabled apache2 config scripts are found in /etc/apache2/conf-enabled/
. You can create a new file with the name of your service. For example studyarea.conf
.
<VirtualHost *:80>
DocumentRoot /home/pi/www/html/
# Enable proxying WebSockets
ProxyPass /ws ws://localhost:8001/
# Enable proxying HTTP
ProxyPass /ws http://localhost:8080/
</VirtualHost>
Because VirtualHost is listening on port 80, we can use the same port for the webserver and the websockets. The ProxyPass
is used to proxy the websocket. The /ws
is the path where the websockets are hosted. Its hosted on localhost/ws
so it kinda creates a new subdomain for the websocket. The localhost:8001
is the address of the websockets and the localhost:8080
is the address of the webserver.
DocumentRoot
is the path where the files are for the website.
Sources:
- https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
- https://www.serverlab.ca/tutorials/linux/web-servers-linux/how-to-reverse-proxy-websockets-with-apache-2-4/
- https://gist.github.com/mortenege/91ec6fe02dca6f736303a00f8cea2731
- https://www.jscape.com/blog/forward-proxy-vs-reverse-proxy#:~:text=While%20a%20forward%20proxy%20proxies,is%20providing%20file%20transfer%20services.
- https://webmasters.stackexchange.com/questions/143106/functional-difference-between-proxypass-and-proxypassreverse-in-apache