【问题标题】:Configuring Virtual Hosts Apache配置虚拟主机 Apache
【发布时间】:2011-10-13 18:22:35
【问题描述】:
我一直在尝试将我的 apache 服务器配置为支持虚拟主机,这些主机然后会将在端口 80 发出的任何请求重定向到托管在 Jboss AS 中的不同应用程序,因此例如我的配置将是这样的:
<VirtualHost *:80>
ServerName www.testdomain.com
ProxyPass / http://localhost:8080/contextPath
ProxyPassReverse / http://localhost:8080/contextPath
ProxyPreserveHost On
ProxyPassReverseCookiePath / /
</VirtualHost>
但是问题是当我尝试访问http://www.testdomain.com 时,url 被有效地重定向到 localhost:8080,但是,我得到了一个重复的上下文路径。即:http://www.testdomain.com/contextPath/contextPath.
任何想法为什么会发生这种情况。非常感谢。
【问题讨论】:
标签:
java
redirect
jboss
virtualhost
contextpath
【解决方案1】:
您需要删除“ProxyPass”和“ProxyPassReverse”条目,除非您确实尝试代理某些内容。如果 jboss AS 在另一台服务器上,那么您需要保留代理条目,但在我看来,如果内容在一台机器上而不是多台机器上,您可能会觉得太难了。
如果您希望一台服务器使用不同的基本文件夹作为 2 个不同域的根目录,则需要通过指定 DocumentRoot 参数来配置这 2 个域。
例如,如果我想在一台计算机上托管 google.com 和 yahoo.com,我的虚拟主机条目将包含:
<VirtualHost *:80>
ServerName www.google.com
DocumentRoot /var/www/Google
</VirtualHost>
<VirtualHost *:80>
ServerName www.yahoo.com
DocumentRoot /var/www/Yahoo
</VirtualHost>
然后,每个服务器的根目录将分别进入 google 文件夹和 yahoo 文件夹。
如果您尝试代理完全不同的机器,那么以下应该可以工作:
<VirtualHost *:80>
ServerName www.google.com
ProxyPass / www.google.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.yahoo.com
ProxyPass / www.yahoo.com
</VirtualHost>
您的输入特别没有多大意义。我认为它应该是这样的:
<VirtualHost *:80>
ServerName www.testdomain.com
ProxyPass /contextPath http://localhost:8080
ProxyPassReverse /contextPath http://localhost:8080
</VirtualHost>
【解决方案2】:
我遇到了同样的问题,通过在网址中添加正斜杠解决了这个问题。
ProxyPass / http://localhost:8080/contextPath/
ProxyPassReverse / http://localhost:8080/contextPath/
这为我解决了!
单个虚拟主机文件的完整示例。我有几个,每个域和子域一个。
ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName enter.name.here
ProxyPass / http://127.0.0.1:8080/<contextPath>/
ProxyPassReverse / http://127.0.0.1:8080/<contextPath>/
ErrorLog /var/log/apache2/somelog.log
CustomLog /var/log/apache2/somecustom.log common
</VirtualHost>