lovasoa / SQLpage

SQL-only webapp builder, empowering data analysts to build websites and applications quickly
https://sql.ophir.dev
MIT License
883 stars 64 forks source link

Two applications on the same server #288

Closed DSMejantel closed 2 months ago

DSMejantel commented 2 months ago

Introduction

When we want to launch 2 differents appli with SQLpage with 2 differents ports for example 8080 and 8081 and behind 2 https domain it's impossible to reach the server

Apr 11 20:28:30 ns376272 sqlpage.bin[1546]: [2024-04-11T18:28:30.819Z INFO  sqlpage] Server started successfully.
Apr 11 20:28:30 ns376272 sqlpage.bin[1546]:         SQLPage is now running on https://rucher.dsoulie.fr/
Apr 11 20:28:30 ns376272 sqlpage.bin[1546]:         You can write your website's code in .sql files in /var/www1/RUCHER.
Apr 11 20:28:30 ns376272 sqlpage.bin[1546]: [2024-04-11T18:28:30.819Z ERROR sqlpage] Another program is already using port 8081 (maybe another i>
Apr 11 20:28:30 ns376272 sqlpage.bin[1546]:
Apr 11 20:28:30 ns376272 sqlpage.bin[1546]:     Caused by:
Apr 11 20:28:30 ns376272 sqlpage.bin[1546]:         Address already in use (os error 98)
lovasoa commented 2 months ago

Running two applications on the same server

Without HTTPS, on different ports

You can run the two applications by specifying different values for the port configuration variable for the two applications, and omitting https_domain.

On the same port

If you want to run both applications on the same port (HTTPS on port 443, or HTTP on port 80) for instance, you will need to put a reverse proxy such as nginx between the public web and your application.

The reverse proxy will receive requests, and based on their contents, decide which SQLPage instance to forward them to.

Example nginx configuration

HTTP configuration for site1 (site1-http.conf):

server {
    listen 80;
    server_name site1.com;

    location / {
        proxy_pass http://localhost:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

HTTP configuration for site2 (site2-http.conf):

server {
    listen 80;
    server_name site2.com;

    location / {
        proxy_pass http://localhost:8082;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Note the logs in the post above demonstrate a bug in sqlpage when trying to listen on both https and http (when the configuration file contains both a non-443 port and an https_domain entry). This is not related to multisite installations, but I will try to fix it soon. The bug is in https://github.com/lovasoa/SQLpage/blob/main/src/webserver/http.rs#L523-L529 SQLPage tries to bind to the http port twice instead of binding once to the http port and once to the https port.