【问题标题】:Multiple domains, multiple vhosts, separate rails apps多个域、多个虚拟主机、单独的 Rails 应用程序
【发布时间】:2011-09-24 14:47:59
【问题描述】:

我正在尝试使用多个域在同一个 VPS 上托管多个不同的 Rails 应用程序。我在 Ubuntu 10.10 上使用 Apache 2.2.17。对于 Apache,我有多个 vhost 文件,因此我可以轻松启用和禁用特定站点,而无需将它们注释掉或删除它们。此外,我还使用了 mod_rewrite,这样访问同一个 rails 应用程序的多个域似乎会访问同一个 URL,因此我不会因为重复内容而受到搜索引擎的打击。

我相信我的 DNS 设置正确。对于每个域,我都有一个 www 子域以及一些特定于站点的子域,例如博客等。我看到的问题是 Apache 似乎立即匹配 www 子域而不检查它后面的附加 URL。更改 ServerAlias 无济于事。例如,使用我的设置,如果我输入davidheartsrachel.com,我会正确访问我的婚礼网站。但是,如果我使用www.davidheartsrachel.com,我会访问我的另一个网站,我的软件开发业务。 URL 不会被重写;它保持为davidheartsrachel.com 而不是afewguyscoding.com

我能够使其正常工作的唯一方法是在主 vhost 文件中使用 mod_rewrite 来重定向到婚礼网站 vhost 文件(你可以看到我在主 vhost 文件中有它,但它是出于此问题的目的已注释掉)。这对我来说似乎不合适?我应该获得另一个 IP 并使用 IP 虚拟主机而不是基于名称的虚拟主机吗?

当我执行apachectl -S 时,我得到以下信息:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
         port 80 namevhost afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
         port 80 namevhost blog.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:69)
         port 80 namevhost lbbs.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:84)
         port 80 namevhost davidheartsrachel.com (/etc/apache2/sites-enabled/davidheartsrachel.com:1)
         port 80 namevhost dhr.afewguyscoding.com (/etc/apache2/sites-enabled/davidheartsrachel_staging:1)
Syntax OK

我注意到afewguyscoding.com 是默认站点 - 但是,它不需要进行完整的字符串匹配来确定正确的站点吗?

主站点的虚拟主机文件

<VirtualHost *:80>
  ServerAdmin david.stites@afewguyscoding.com
  ServerName afewguyscoding.com
  ServerAlias davidstites.com, 5280software.com, milehigh-software.com, milehighsoftware.org
  ServerAlias www.5280software.com, www.milehigh-software.com, www.milehighsoftware.org, www.davidstites.com, www.afewguyscoding.com

  # this tells rails that it will run in production mode
  # this is for rails < 3.x
  RailsEnv production

  DocumentRoot /var/www/afewguyscoding/current/public
  DirectoryIndex index.html

  # custom log file locations
  # possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel alert
  ErrorLog  /var/www/afewguyscoding/current/log/error.log
  CustomLog /var/www/afewguyscoding/current/log/access.log combined

  # allows compression of text based mime.types
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

  FileETag None
  RewriteEngine On

  # check for maintenance file and redirect all requests
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
  RewriteCond %{REQUEST_URI} !^/ws/
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html

  #RewriteCond %{HTTP_HOST} ^www.davidheartsrachel.com$
  #RewriteRule ^(.*)$ http://davidheartsrachel.com$1 [L]

  RewriteCond %{HTTP_HOST} ^www.davidstites.com$
  RewriteRule ^(.*)$ http://www.afewguyscoding.com$1 [R=301,L]

  <Directory /var/www/afewguyscoding/current/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  # In case I ever need CGI
  #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  #<Directory "/usr/lib/cgi-bin">
  #     AllowOverride None
  #     Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  #     Order allow,deny
  #     Allow from all
  #</Directory>

  # how we can restrict access to documents from the local subnet
  #Order deny,allow
  #Deny from all
  #Allow from 127.0.0.0/255.0.0.0 ::1/128

  <Location /blog>
    PassengerEnabled off
  </Location>
</VirtualHost>

<VirtualHost *:80>
  ServerAdmin info@afewguyscoding.com
  ServerName blog.afewguyscoding.com

  DocumentRoot /var/www/wpress
  DirectoryIndex index.php

  <Directory /var/www/afewguyscoding/current/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

婚礼现场的虚拟主机文件

<VirtualHost *:80>
  ServerAdmin info@davidheartsrachel.com
  ServerName davidheartsrachel.com
  ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com
  ServerAlias www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net

  # this tells rails that it will run in production mode
  # this is for rails < 3.x
  RailsEnv production
  # this is for rails >= 3.x
  RackEnv production

  DocumentRoot /var/www/davidheartsrachel/current/public
  DirectoryIndex index.html

  # Custom log file locations
  # Possible values include: debug, info, notice, warn, error, crit, alert and emerg,
  LogLevel alert
  ErrorLog  /var/www/davidheartsrachel/current/log/error.log
  CustomLog /var/www/davidheartsrachel/current/log/access.log combined

  # Allows compression of text based mime types
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

  FileETag None
  RewriteEngine On

  # Check for maintenance file and redirect all requests
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]

  RewriteCond %{HTTP_HOST} ^www.rachelanddavidwedding.com$
  RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]

  RewriteCond %{HTTP_HOST} ^www.rachelanddavidstites.com$
  RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]

  # Static cache
  RewriteCond %{REQUEST_METHOD} !^POST$
  RewriteCond /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html -f
  RewriteRule ^(.*)$ /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html [L]

  <Directory /var/www/davidheartsrachel/current/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from All
  </Directory>
</VirtualHost>

如果您能想到任何可能有帮助的更多信息,请让我发布。

编辑:

让我试着澄清一下。在最本质的形式中,我的问题是:为什么要去 davidheartsrachel.com 会带你到 www.afewguyscoding.com 而 davidheartsrachel.com 会根据我上面的配置带你到 davidheartsrachel.com

【问题讨论】:

    标签: ruby-on-rails apache dns vhosts


    【解决方案1】:

    答案是我错误地将 ServerAlias 下的条目用逗号分隔:

    ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com, www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net
    

    应该是空格:

    ServerAlias rachelanddavid.net rachelanddavidstites.com rachelanddavidwedding.com www.davidheartsrachel.com www.rachelanddavidstites.com www.rachelanddavidwedding.com www.rachelanddavid.net
    

    【讨论】:

      【解决方案2】:

      在我的服务器上,我没有在我的主 httpd.conf 文件中定义主站点。我在单独的应用程序特定的 conf 文件中完成所有虚拟主机。

      我的主要 httpd.conf 中唯一的内容是 NameVirtualHost *:80 这一行

      Here's a gist 你可以试试。我从您的主站点配置中提取了所有婚礼站点配置。我将其设置为 davidstites.com、afewguyscoding.com 和 www.davidstites.com 都重定向到 www.afewguyscoding.com。其他域(milehigh 等)不受影响。我不确定你想让它们去哪里。

      所有与婚礼相关的域都重定向到 www.davidheartsrachel.com。

      我确实注意到我认为您的主站点配置可能存在问题。在您设置目录权限的最后,您使用的是 /var/www/afewguyscoding/current/public 目录,而不是 DocumentRoot 的 /var/www/wpress。我在那里所做的更正位于该要点的第一个文档的第 74 行。

      【讨论】:

      • 因此重定向适用于应该组合在一起的网站。但是,我不想为完全不同的网站进行 301 重定向,即在我的情况下,我的软件网站有一个虚拟主机,我的婚礼网站有一个虚拟主机。在另一个 vhost 文件中为完全独立的 vhost 混合重定向似乎是错误的。
      • 定义不应在自己的虚拟主机中组合在一起的网站。没有重定向。我在服务器上有多达 30 个域,有些组合在一起,有些则没有。我每台服务器只有一个 IP 地址。
      • 这就是我所做的,如果你看看我的原始帖子。注意两个不同的 vhost 文件。问题是不知何故,它与完整的 URL 不匹配 - 它看到 www 并自动选择“主要”虚拟主机 afewguyscoding.com,而不管 www 后面的 TLD 是什么。
      • 我想我不明白你为什么要将婚礼网站虚拟主机配置混合到主网站配置中。
      • 尝试不这样做。但是“我看到的问题是 Apache 似乎立即匹配 afewguyscoding.com 的 www 子域,而不是检查它后面的附加 URL。更改 ServerAlias 没有任何作用。例如,使用我的设置,如果我输入 davidheartsrachel.com,我“混合它们是我可以使其正常工作的唯一方法。我试图让它们在不混合它们的情况下工作。
      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 2023-03-28
      • 2023-04-10
      • 2016-03-31
      • 2014-12-20
      • 2015-11-29
      相关资源
      最近更新 更多