Redirect to another Port

I have a WE app that is complied and running on MAC OS using XOJO 2022. This uses a secure port and all traffic to that server is redirecting http (80) to 443 (https) inside Apache2. This XOJO WE app uses a different secure port let’s call it 1234. I have been reading Apache2 docs on how to redirect to another port and have never made this work.

The server name = sub1.mydomain.com and is available public and internal. Users currently access using sub1.mydomain.com:1234. I am looking to enable sub1.mydomain.com to automatically redirect through a conf file in etc/apache2/sites-enabled/ to redirect to sub1.mydomain.com:1234

Some aspects of https.conf that are set - these are not in order - they are included so you know what has been enabled to allow redirect etc:

DocumentRoot "/Library/WebServer/Documents"
# Include sites-available
# Change when not using
Include /etc/apache2/sites-available/thissite.conf
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_connect_module libexec/apache2/mod_proxy_connect.so
LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so

Here is what is inside my other sites conf file

RewriteEngine on
<VirtualHost *:80>
  	ServerAdmin administrator@mydomain.com
    ServerName sub1.mydomain.com
	Redirect / https://www. sub1.mydomain.com
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin administrator@ sub1.mydomain.com
    ServerName sub1.mydomain.com
    ProxyPreserveHost On

    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:1234/
    ProxyPassReverse / http://localhost:1234/
</VirtualHost>

Not being an expert on this and instead of coming close but no cigar enough times I think it is time to see what I am not doing correctly.

With this change to the .conf file

<VirtualHost *:80> 
  ServerName sub1.mydomain.com
  ServerAlias www. sub1.mydomain.com

  Redirect permanent / https://sub1.mydomain.com:1234/
</VirtualHost>

<VirtualHost *:443> 
  ServerName sub1.mydomain.com
  ServerAlias www. sub1.mydomain.com

  Redirect permanent / https://sub1.mydomain.com:1234/
</VirtualHost>

And if I use http://sub1.mydomain.com:80 I get a web page redirect to the correct login web page from the WE app. I also noticed that :1234 appears in the URL and I would like to keep that hidden.

If I leave off :80 and type https://sub1.mydomain.com you receive the message

This site can’t be reached

** sub1.mydomain.com** refused to connect.

If I type sub1.mydomain.com:1234 the site works.

Here’s what I use for port redirection, this example redirects to 8081. This allows me to run multiple Xojo web apps on the same server - just need to specify a different port for each.

<VirtualHost *:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
ProxyPreserveHost on
ProxyRequests on
ProxyPass / http://localhost:8081/
ProxyPassReverse / http://localhost:8081/
</VirtualHost>

I use certbot to obtain a Let’s Encrypt certificate and configure the secure site. I’m not sure your existing 80 to 443 redirection will be compatible with this. My server runs Ubuntu.

1 Like

Thanks - that works great if you type http://sub1.mydoamin.com in the browser. This resolves but also writes :port Hiding the port is something I would like to do to hide the port.

However when you write https://sub1.mydoamin.com the request fails to connect. To attempt to resolve I simply took the original … </Virtual Host> and replaced *:80 with *:443 and that fails to work. This is the message on the web page -

This site can’t be reached

.

On another note I am asking because I am trying to learn this aspect.

How can you use that over and over again for different apps? In order to do that the domain identified in the DNS would need to be different such as:

sub1.mydomain.com
sub2.mydomain.com

If the same ServerName then you would request a different folder such as

sub1.mydomain.com/app1
sub1.mydomain.com/app2

Then somehow a test would need to check the folder request in order to redirect to that folder.

Or is there a keyword read in the header that you use to identify in

Virtual Host

? I just recognized as I was writing this the < Virtual Host > disappears when you remove the leading and trailing white space.

Which are correct and how would that be written?

I think more or less your config looks fine. You will want to configure more things, but this should be a good starting point:

<VirtualHost *:443>
    # ...
    ServerName sub1.mydomain.com
    SSLEngine On
    SSLCertificateFile /path/to/your/certificate/file.pem
    ProxyPass / http://127.0.0.1:1234/ # Your Xojo App
    ProxyPassReverse / http://127.0.0.1:1234/ # Your Xojo App
    # ...
</VirtualHost>

If you’re using Apache and it’s handling SSL already, your Xojo app doesn’t needs to do it again.

While this blog post uses Nginx, the concepts will also apply to Apache:

Using multiple VirtualHost, you can serve applications behind different domains and subdomains.

That said, using Xojo Cloud or @Tim_Parnell’s Lifeboat will make your life easier. If your hosting provider has an admin panel like Plesk or CPanel, you’ll probably be able to configure all this without having to deal with config files.

1 Like

Thanks. When I install that after the Virtual 80 block this works when the URL is a :80 request. I have XOJO set to implement SSL so I will redo and allow the website to handle SSL requests and see what happens

I use a managed VPS and the support staff has set up a reverse proxy for each one of my web apps. I have no idea how to do it so all I can suggest is to research reverse proxies.