0

I need your help.

I have 2 versions of the application running on the Linux machine. Version1 runs on localhost port 5000, version2 runs on localhost port 5001.

I need users to be able to access either of the application versions depending on the URL they use.

My existing Apache httpd.conf as follows:

When users type http:/my.company.com it redirects them to HTTPS port 443 and port 443 redirects to localhost 5000. This works.

<VirtualHost *:80>                                                                                                                                          
ServerName my.company.com                                                                                                                  
Redirect / https://my.company.com/                                                                                                                 
</VirtualHost>                                                                                                                                                                  

<VirtualHost *:443>                                                                                                                                        
ServerName my.company.com/                                                                                                                 
TimeOut 600                                                                                                                                                     
ProxyPreserveHost On                                                                                                                                            
ProxyRequests Off                                                                                                                                            
ProxyPass / http://localhost:5000/                                                                                                                           
ProxyPassReverse / http://localhost:5000/                                                                                                                
SSLEngine on                                                                                                                                                     
SSLOptions +StrictRequire                                                                                                                                
SSLCertificateFile /etc/pki/tls/certs/my.company.crt                                                                                                               
SSLCertificateKeyFile /etc/pki/tls/private/my.company.key                                                                                     
</VirtualHost>  

Now I need to make it when users type http://my.company.com:15000 it redirects them to some HTTPS port that redirects to localhost 5001 where my application is listening.

If there is another way to do this, like use a different domain name like my-v2.company.com it would also work. I can create a new DNS entry. The idea is that when users use a different port or a different domain it should redirect them to a different application port.

I tried searching here and tried multiple setups and cannot make it work this way.

Dave M
  • 4,514
  • 22
  • 31
  • 30
housemd
  • 15
  • 2
  • 4

1 Answers1

3

You can define multiple VirtualHosts for the same port, you just have to set different ServerName directives for each.

If you want my.company.com to be the application at port 5000, and my-v2.company.com to be the application at port 5001, I'd suggest that you go with a configuration like this:

# Enable name-based routing on port 80 and port 443
NameVirtualHost *:80
NameVirtualHost *:443

# Redirect HTTP -> HTTPS on my.company.com
<VirtualHost *:80>
ServerName my.company.com
Redirect / https://my.company.com/
</VirtualHost>

# Redirect HTTP -> HTTPS on my-v2.company.com
<VirtualHost *:80>
ServerName my-v2.company.com
Redirect / https://my-v2.company.com/
</VirtualHost>

# Serve the application on port 5000 to https://my.company.com
<VirtualHost *:443>
ServerName my.company.com
TimeOut 600
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/pki/tls/certs/my.company.crt
SSLCertificateKeyFile /etc/pki/tls/private/my.company.key
</VirtualHost>

# Serve the application on port 5001 to https://my-v2.company.com
<VirtualHost *:443>
ServerName my-v2.company.com
TimeOut 600
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:5001/
ProxyPassReverse / http://localhost:5001/
SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/pki/tls/certs/my-v2.company.crt
SSLCertificateKeyFile /etc/pki/tls/private/my-v2.company.key
</VirtualHost>

The real magic about this answer is the first two lines, which tell Apache to allow creating two VirtualHosts on the same port and to differentiate them by the ServerName that has been set.

Tim Schumacher
  • 576
  • 3
  • 12
  • thanks, i modified the file and restarted the server. now both URL resolve to version 1. in DNS I have both URL resolve to the same external IP address of the machine. any thoughts? – housemd Jan 05 '20 at 20:24
  • or do they need to reference 2 separate external IP addresses that link to the same server on DNS side? thanks – housemd Jan 05 '20 at 20:47
  • @housemd If the solution works, please upvote the answer/mark the answer as accepted. – Tim Schumacher Jan 06 '20 at 00:20