2011-08-04 167 views
4

我有一个Java应用程序响应多个域,并为每个域使用特定的apache虚拟主机。这是因为Apache比tomcat更快,可以提供静态资源。apache虚拟主机和“动态”域

需要在运行时执行此操作,而无需重新启动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.comto domain.com的符号链接是个好主意吗?

存在另一种方式来做到这一点?我在apache管理方面真的很差...

非常感谢!

Ciao,Davide。

回答

2

我经常使用的符号链接做类似这样的配置时,多个域链接到同一Web根目录,存在这样的解决方案没有明确的危害如此绝对没有理由避开它。

0

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

关闭我的头顶,你可以使用类似:

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

那会,如果您使用SSL,但会导致问题由于硬编码http://。我想你可以在重写规则行更改为以下,以避免:

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