未find处理网站,找不到dynamic的大容量虚拟主机

我最近在Apache中设置了大容量虚拟主机 ,所以我们需要做的就是创build一个目录来创build一个新的虚拟主机。 然后,我们还使用通配符DNS将所有子域映射到运行我们的Apache实例的服务器。

这工作出色,但是我现在有麻烦configuration它故障转移到适当的默认/错误页面,当虚拟主机目录不存在。

这个问题似乎是由我处理两个错误条件的愿望混合在一起的:

  1. 没有findvhost,即没有find与HTTP主机头中提供的主机匹配的目录。 我想这个显示一个合适的网站没有find错误页面。
  2. 404页面找不到虚拟主机的情况。

另外我有一个专门的“API”虚拟主机在自己的虚拟主机。

我尝试了一些变化,似乎没有performance出我想要的行为。 以下是我现在正在处理的内容:

NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot /var/www/site-not-found ServerName sitenotfound.mydomain.org ErrorDocument 500 /500.html ErrorDocument 404 /500.html </VirtualHost> <VirtualHost *:80> ServerName api.mydomain.org DocumentRoot /var/www/vhosts/api.mydomain.org/current # other directives, eg setting up passenger/rails etc... </VirtualHost> <VirtualHost *:80> # get the server name from the Host: header UseCanonicalName Off VirtualDocumentRoot /var/www/vhosts/%0/current # other directives ... eg proxy passing to api etc... ErrorDocument 404 /404.html </VirtualHost> 

我的理解是,第一个虚拟主机块被用作默认,所以我有这个在这里作为我赶上所有的网站。 接下来我有我的API虚拟主机,然后最后我的虚拟主机块。

因此,对于与前两个ServerName不匹配的域,并且在/var/www/vhosts/没有对应的目录,我希望它会落到第一个虚拟主机上,但是通过此设置,所有域都parsing为我的默认网站未find。 为什么是这样?

通过首先放置大规模的虚拟主机块,我可以让大众虚拟主机来解决,但不是我的网站,找不到虚拟主机…在这种情况下,我似乎无法find一种方法来区分页面在虚拟主机中的级别404,以及VirtualDocumentRoot无法find虚拟主机目录的情况(这似乎也使用404)。

任何帮助这个绑定非常感谢!

我希望它倒下到第一个虚拟主机,但是通过这个设置,所有的域名parsing到我的默认站点 – 找不到。 为什么是这样?

在不同的虚拟主机之间不存在故障切换逻辑 – 一旦请求被分配给一个虚拟主机,它就是最终的。

如果在dynamic虚拟主机块中没有ServerNameServerAlias ,则取决于“要加载的第一个虚拟主机是缺省”行为,以向该虚拟主机分配请求。 当它不是第一个加载的虚拟主机时,它基本上是惰性的。 没有办法让它得到请求。


我对如何处理这个问题的build议是让“找不到网站”的行为是代理或redirect到一个工作网站(然后提供一个“这里没有内容!”页面),而不是404的变体页。

sitenotfound.mydomain.org虚拟主机向下移动,并将dynamic虚拟主机返回到顶端,以使其成为默认设置。 我们将使用它来提供我们友好的“无网站”页面。

然后,让dynamic虚拟主机能够在提供内容之前检查网站是否存在。 在其虚拟主机中添加:

 RewriteEngine On # If there's no directory where it should be for this host.. RewriteCond /var/www/vhosts/%{HTTP_HOST} !-d # (or a symlink, we'll be ok with those too) RewriteCond /var/www/vhosts/%{HTTP_HOST} !-l # Then, we'll redirect the user to our friendly "no site here" page.. # Note that I'm setting it to 302 so that they won't be cached, since # you might trigger this accidentally just before a new site goes live.. RewriteRule ^ http://sitenotfound.mydomain.org/invalid-site.html [R=302,L] # Or if you wanted to proxy instead of redirecting, use this instead: #RewriteRule ^ http://sitenotfound.mydomain.org/invalid-site.html [P,L] 

因此,dynamic主机现在将检查网站目录是否存在,然后redirect或将用户代理到友好的说明页面。 这种行为听起来是否适合您的需求?