我在apache上有一个运行3站点的linux服务器。我们称它们为railsap1、railsap2和simpleapp。两个rails应用程序都使用了mongrel集群。另一个应用程序只是一个html文件。我在apache中为每个站点设置了不同的虚拟主机文件,并为两个rails站点设置了mongrel_cluster.yml文件(所有这些的代码都在底部)。
通过所有设置,我可以很好地启用apache中的站点。我可以很好地为每个rails站点启动mongrel集群。而且,事实上,在我的浏览器中访问www.simpleapp.com和www.railsapp1.com效果很好。然而,www.railsapp2.com给了我很多麻烦。服务器不会显示railsapp2的代码,而是返回railsapp1的html。如果我在apache中禁用railsapp1,然后转到www.railsapp2.com,服务器现在将返回simpleapp的html。只有在apache中同时禁用railsapp1和railsapp2,服务器才能正确响应www.railsapp2.com上的请求。
有没有想过为什么会这样?
SimpleApp的vhost文件:

<VirtualHost *:80>
  ServerName www.simpleapp.com
  ServerAlias simpleapp.com
  DocumentRoot /home/nudecanaltroll/public_html/simpleapp
</VirtualHost>

RailsApp1的vhost文件:
<VirtualHost *:80>
  ServerName railsapp1.com
  DocumentRoot /home/nudecanaltroll/public_html/railsapp1/public
  RewriteEngine On
  <Proxy balancer://mongrel1>
    BalancerMember http://127.0.0.1:5000
    BalancerMember http://127.0.0.1:5001
    BalancerMember http://127.0.0.1:5002
  </Proxy>
  # Timeout in 30 seconds
  ProxyTimeout 30
  # Make sure people go to www.railsapp1.com, not railsapp1.com
  RewriteCond %{HTTP_HOST} ^railsapp1\.com$ [NC]
  RewriteRule ^(.*)$ http://www.railsapp1.com$1 [R=301,NE,L]
  # Redirect all non-static requests to thin
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/mongrel1(.*)$ balancer://mongrel1%{REQUEST_URI} [P,QSA,L]
  # Proxy Stuff
  ProxyPass / balancer://mongrel1/
  ProxyPassReverse / balancer://mongrel1/
  ProxyPreserveHost on
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  # Custom log file locations
  ErrorLog  /home/nudecanaltroll/public_html/railsapp1/log/error.log
  CustomLog /home/nudecanaltroll/public_html/railsapp1/log/access.log combined
</VirtualHost>

RailsApp2的vhost文件:
<VirtualHost *:80>
  ServerName railsapp2.com
  DocumentRoot /home/nudecanaltroll/public_html/railsapp2/public
  RewriteEngine On
  <Proxy balancer://mongrel2>
    BalancerMember http://127.0.0.1:6000
    BalancerMember http://127.0.0.1:6001
    BalancerMember http://127.0.0.1:6002
  </Proxy>
  # Timeout in 30 seconds
  ProxyTimeout 30
  # Make sure people go to www.railsapp2.com, not railsapp2.com
  RewriteCond %{HTTP_HOST} ^railsapp2\.com$ [NC]
  RewriteRule ^(.*)$ http://www.railsapp2.com$1 [R=301,NE,L]
  # Redirect all non-static requests to thin
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/mongrel2(.*)$ balancer://mongrel2%{REQUEST_URI} [P,QSA,L]
  # Proxy Stuff
  ProxyPass / balancer://mongrel2/
  ProxyPassReverse / balancer://mongrel2/
  ProxyPreserveHost on
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  # Custom log file locations
  ErrorLog  /home/nudecanaltroll/public_html/railsapp2/log/error.log
  CustomLog /home/nudecanaltroll/public_html/railsapp2/log/access.log combined
</VirtualHost>

Railsapp1的mongrel_cluster.yml文件:
---
address: 127.0.0.1
log_file: log/mongrel.log
port: 5000
cwd: /home/nudecanaltroll/public_html/railsapp1
environment: production
pid_file: /home/nudecanaltroll/public_html/railsapp1/tmp/pids/mongrel.pid
servers: 3

Railsapp2的mongrel_cluster.yml文件:
---
address: 127.0.0.1
log_file: log/mongrel.log
port: 6000
cwd: /home/nudecanaltroll/public_html/railsapp2
environment: production
pid_file: /home/nudecanaltroll/public_html/railsapp2/tmp/pids/mongrel.pid
servers: 3

最佳答案

我想出来了。出于未知原因,我需要为railsap2设置serveralias,并在servername前面添加“www.”。因此railsapp2.com vhost文件的顶部现在看起来如下:

<VirtualHost *:80>
  ServerName www.railsapp2.com
  ServerAlias railsapp2.com
  ...

出于某种原因,railsapp1不要求这些更改才能正常工作。

关于ruby-on-rails - 在Apache和Mongrel上为Rails应用设置虚拟主机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3670273/

10-11 23:20