![Apache 从同一个域为多个 ruby on rails 应用程序提供服务](https://linux22.com/image/755565/Apache%20%E4%BB%8E%E5%90%8C%E4%B8%80%E4%B8%AA%E5%9F%9F%E4%B8%BA%E5%A4%9A%E4%B8%AA%20ruby%E2%80%8B%E2%80%8B%20on%20rails%20%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E6%8F%90%E4%BE%9B%E6%9C%8D%E5%8A%A1.png)
我想通过单个域使用 Apache 和 Passenger 为多个不同的 ruby on rails 应用程序提供服务。所有应用程序都应使用相同的证书,并且我希望默认使用 HTTPS 来交付应用程序。
# single domain
domain.example.com
# applications accessible via branches
domain.example.com/app_one
domain.example.com/app_two
我为每个应用程序都进行了以下配置,这些配置运行良好,但只能为单个应用程序提供服务。当我启用多个这样的 Apache 站点配置时,按字母顺序排列的第一个应用程序将响应,其余应用程序则不起作用。
#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>
我还让 rails 应用程序响应单独的子分支:
# rails app config.ru
map '/app_one' do
run AppName::Application
end
我想我必须根据配置创建子分支。但是我不知道该怎么做。我还尝试将配置打包成一个Location
块。但是,这样做有限制,并且不接受我的所有配置。最后,我再次恢复了配置,因为我的配置无效。如果有人能帮我解决这个问题,那就太好了。
答案1
最后,我找到了一个基本可行的解决方案。对于有同样目标的其他人来说。我现在只使用两个配置文件。一个用于默认 http,一个用于 https。然而,还有一个问题。Apache 总是从与乘客从文档根信息中选择的相同的资产文件夹中提供服务。我不知道如何防止这种情况。
# 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>