ruby-on-rails - 在 Apache 和 Mongrel 上为 Rails 应用设置虚拟主机

标签 ruby-on-rails linux apache mongrel virtualhost

我有一个 Linux 服务器在 Apache 上运行 3 个站点。我们称它们为 RailsApp1、RailsApp2 和 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

最佳答案

我想通了。由于我不知道的原因,我需要为 RailsApp2 设置 ServerAlias,并添加“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/

相关文章:

ruby-on-rails - Nginx ssl_protocol 设置不起作用

linux - UTF-8 输入和使用 XGetICValues

apache - 无法提交到 SVN(路径 ... 已存在)

php - PCRE 库版本太旧

java - Apache Axis : no containing element

ruby-on-rails - 迁移数据库和种子时出现 Ruby 错误

ruby-on-rails - 在 Rails 中测试 Rake : Multiple Error Raises Silenced In Test

ruby-on-rails - 有没有办法在 Rails 控制台内的输出上实现类似 grep 的功能

c - 特定时间的 sem_timedwait()

c - 获取系统 api 调用执行的命令的 pid