0

I want to serve multiple different ruby on rails applications using Apache and Passenger via a single domain. The applications all should use the same certificate and I want to deliver the apps using HTTPS as default.

# single domain
domain.example.com
# applications accessible via branches
domain.example.com/app_one
domain.example.com/app_two

I have the following configuration for each of the applications which works fine however only to serve a single application. When I enable multiple of these Apache site configurations then the first application in order of alphabet will answer and the rest does not work.

#app_one.conf

<VirtualHost *:80>
    ServerName domain.example.com/app_one
    Alias /app_one /var/www/app_one/public
    DocumentRoot /var/www/app_one/public
    PassengerRuby /usr/local/rvm/gems/ruby-2.3.8/wrappers/ruby

    <Directory /var/www/app_one/public>
      Allow from all
      Options -MultiViews
      Require all granted
    </Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.example.com/app_one
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>
#app_one-secure.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName domain.example.com/app_one
    Alias /app_one /var/www/app_one/public
    DocumentRoot /var/www/app_one/public
    PassengerRuby /usr/local/rvm/gems/ruby-2.3.8/wrappers/ruby

    <Directory /var/www/app_one/public>
      Allow from all
      Options -MultiViews
      Require all granted
    </Directory>

SSLCertificateFile      /etc/encrypt/cert.pem
SSLCertificateKeyFile   /etc/encrypt/key.pem
SSLCertificateChainFile /etc/encrypt/keychain.pem

Include /etc/encrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

I also made the rails applications respond to the separate sub-branches:

# rails app config.ru
map '/app_one' do
  run AppName::Application
end

I guess I have to make the configuration sub-branch depending. However I have no clue on how to do this. I also tried to pack the configuration into a Location block. However that is restrictive and does not accept all of my configuration. In the end I reverted this again as my configuration was invalid. It would be really nice if somebody you could help me figuring this out.

1 Answers1

0

Finally I found a mostly working solution. For anybody else who has the same goal. I use only two configuration files now. One for the default http and one for https. However one problem remains. Apache always serves from the same assets folder as passenger picks them from document root information. I have no idea how to prevent this.

# 000-default.conf

<VirtualHost *:80>
  # general setup
  
  # name of the server
  ServerName my.server.com

  # ruby for passenger
  PassengerRuby /usr/local/rvm/gems/ruby-2.3.8/wrappers/ruby

  # the default document root which is delivered for the server name
  DocumentRoot /var/www/apps/app_one/public

  # instance setup 

  ## app_one
  Alias /app_one /var/www/apps/app_one/public

  <Location /app_one>
    PassengerBaseURI /app_one
    PassengerAppRoot /var/www/apps/app_one
  </Location>

  <Directory /var/www/apps/app_one/public>
    Allow from all
    Options -MultiViews
    Require all granted
  </Directory>

  ## app_two
  Alias /app_one /var/www/apps/app_two/public

  <Location /app_two>
    PassengerBaseURI /app_two
    PassengerAppRoot /var/www/apps/app_two
  </Location>

  <Directory /var/www/apps/app_two/public>
    Allow from all
    Options -MultiViews
    Require all granted
  </Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =my.server.com/app_one 
RewriteCond %{SERVER_NAME} =my.server.com/app_two 
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>
# 000-defaults-https.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
  <VirtualHost *:80>
  # general setup
  
  # name of the server
  ServerName my.server.com

  # ruby for passenger
  PassengerRuby /usr/local/rvm/gems/ruby-2.3.8/wrappers/ruby

  # the default document root which is delivered for the server name
  DocumentRoot /var/www/apps/app_one/public

  # instance setup 

  ## app_one
  Alias /app_one /var/www/apps/app_one/public

  <Location /app_one>
    PassengerBaseURI /app_one
    PassengerAppRoot /var/www/apps/app_one
  </Location>

  <Directory /var/www/apps/app_one/public>
    Allow from all
    Options -MultiViews
    Require all granted
  </Directory>

  ## app_two
  Alias /app_one /var/www/apps/app_two/public

  <Location /app_two>
    PassengerBaseURI /app_two
    PassengerAppRoot /var/www/apps/app_two
  </Location>

  <Directory /var/www/apps/app_two/public>
    Allow from all
    Options -MultiViews
    Require all granted
  </Directory>

SSLCertificateFile      /etc/encrypt/cert.pem
SSLCertificateKeyFile   /etc/encrypt/key.pem
SSLCertificateChainFile /etc/encrypt/keychain.pem

Include /etc/encrypt/options-ssl-apache.conf

</VirtualHost>
</IfModule>