Files
2024-02-15 19:55:17 +01:00

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: