多个域、多个虚拟主机、单独的 Rails 应用程序

发布于 2024-11-17 09:01:46 字数 7205 浏览 7 评论 0原文

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

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

我能够正确使其工作的唯一方法是在主虚拟主机文件中使用 mod_rewrite 重定向到婚礼网站虚拟主机文件(您可以看到我在主虚拟主机文件中有该文件,但它被注释掉了这个问题的目的)。这对我来说似乎不太合适?我应该获取另一个 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 是默认站点 - 但是,它不需要进行完整字符串匹配吗确定合适的站点?

主站点的 vhost 文件

<VirtualHost *:80>
  ServerAdmin [email protected]
  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 [email protected]
  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>

婚礼站点的 vhost 文件

<VirtualHost *:80>
  ServerAdmin [email protected]
  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

I am trying to host multiple different rails apps on the same VPS using multiple domains. I am using Apache 2.2.17 on Ubuntu 10.10. For Apache, I have multiple vhost files so that I can enable and disable particular sites easily without needing to comment them out or delete them. In addition, I am also using mod_rewrite so that the multiple domains that go to the same rails app appear to go to the same URL so I don't take a hit with search engines as far as duplicate content.

I believe that my DNS is setup properly. For each of the domains, I have a www subdomain as well as some site-specific sub-domains, such as blogs, etc. The issue I am seeing is that Apache appears to matching the www subdomain immediately and not examining the additional URL behind it. Changing ServerAlias does nothing. For example, using my setup, if I enter davidheartsrachel.com, I properly reach my wedding website. However, if I use www.davidheartsrachel.com, I reach my other website, my software development business. The URL is not re-written; it stays as davidheartsrachel.com instead of afewguyscoding.com.

The only way I have been able to properly get it to work is to use mod_rewrite in the primary vhost file to redirect to the wedding website vhost file (you can see that I have that in the primary vhost file, but it is commented out for purposes of this question). This doesn't seem proper to me? Should I get another IP and do IP vhosts instead of name-based vhosts?

When I execute apachectl -S, I get the following:

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

I notice that afewguyscoding.com is the default site - however, doesn't it have to do a full string match to determine the proper site?

Primary site's vhost file

<VirtualHost *:80>
  ServerAdmin [email protected]
  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 [email protected]
  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>

Wedding site's vhost file

<VirtualHost *:80>
  ServerAdmin [email protected]
  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>

If you can think of any more information that might be helpful, please, ask me to post it.

Edit:

Let me try to clear this up. In the most essential form, my question is: why does going to davidheartsrachel.com takes you to www.afewguyscoding.com and davidheartsrachel.com takes you to davidheartsrachel.com based on my configuration above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-11-24 09:01:46

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

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

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

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

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

On my servers I don't define a primary site in my main httpd.conf file. I do all the virtual hosting in separate application specific conf files.

The only thing I have in my main httpd.conf is the line NameVirtualHost *:80

Here's a gist of a way you could try it. I pulled all the wedding site configuration out of your main site configuration. I set it so davidstites.com, afewguyscoding.com, and www.davidstites.com all redirect to www.afewguyscoding.com. The other domains (milehigh et al) are unaffected. I'm not sure where you wanted those to go.

All the wedding related domains redirect to www.davidheartsrachel.com.

I did notice what I think might have been a mess up in your main site configuration. Toward the end where you set your directory permissions you were using the /var/www/afewguyscoding/current/public directory instead of the /var/www/wpress that is the DocumentRoot. The correction I made there is on line 74 of the first document in that gist.

笑脸一如从前 2024-11-24 09:01:46

答案是我错误地用逗号分隔了 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

The answer is that I was incorrectly separating the entries under ServerAlias with a comma thusly:

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

It is supposed to be a space:

ServerAlias rachelanddavid.net rachelanddavidstites.com rachelanddavidwedding.com www.davidheartsrachel.com www.rachelanddavidstites.com www.rachelanddavidwedding.com www.rachelanddavid.net
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文