【问题标题】:apache virtual host and "Dynamic" Domainsapache 虚拟主机和“动态”域
【发布时间】:2011-08-04 10:24:11
【问题描述】:

我有一个 java 应用程序响应多个域,并为每个域使用一个特定的 apache 虚拟主机。这是因为 Apache 在提供静态资源方面比 tomcat 更快。

需要在运行时执行此操作,而无需重新启动 apache 配置。 要执行此操作,我使用 VirtualDocumentRoot 指令,如下所述:

AddType text/html .html .shtml
AddOutputFilter INCLUDES .html .shtml

NameVirtualHost *:80
UseCanonicalName Off
<VirtualHost *:80>
    ServerName domain.com
    ServerAlias *

    # Define virtual host directory, using entire domain
    VirtualDocumentRoot /path/to/whosts/%0

    # Define directory access
    <Directory "/path/to/whosts/">
        Options -Indexes MultiViews +Includes
        Order allow,deny
        Allow from all
    </Directory>

    # Define Java Proxies
    <Proxy *>
        AddDefaultCharset Off
        Order deny,allow
        Allow from all
    </Proxy>

    # Allow Libs (static resources) to access apache directly
    ProxyPass /libs !
    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
</VirtualHost>

这不好用,因为如果我尝试访问 www.domain.com,与访问 domain.com 不同。

你认为注册一个从 www.domain.com 到 domain.com 的符号链接是个好主意吗???

存在另一种方法吗?我在 apache 管理方面真的很差......

非常感谢!

Ciao,大卫。

【问题讨论】:

    标签: apache dynamic alias virtualhost restart


    【解决方案1】:

    在进行与此类似的配置时,我经常使用符号链接将多个域链接到同一个 webroot,该解决方案没有明显的危害,因此绝对没有理由回避它。

    【讨论】:

      【解决方案2】:

      一个好的方法是决定哪种类型的 URL 应该是规范的,并使用 mod_rewrite 将 URL 重定向到它 - 例如,将请求匹配到 domain.com 并将它们重定向到 www.domain.com。网上有很多关于如何做到这一点的教程,您应该可以轻松找到它们。

      在我的脑海中,你可以使用类似的东西:

      RewriteEngine on
      RewriteCond %{HTTP_HOST} !^www\.$ [NC]
      RewriteRule ^(.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      

      但是,如果您使用 SSL,由于硬编码的 http://,这会导致问题。我认为您可以将 RewriteRule 行更改为以下内容以避免这种情况:

      RewriteRule ^(.*) %{SERVER_PROTOCOL}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      

      【讨论】:

        猜你喜欢
        • 2015-02-24
        • 2014-02-26
        • 2011-10-16
        • 2013-09-08
        • 2017-01-02
        • 2011-09-30
        • 2012-04-27
        • 2018-11-21
        • 1970-01-01
        相关资源
        最近更新 更多